?? dreamtimesnotepad.java
字號:
}
});
JButton toolcopy = new JButton(Iconcopy);
toolcopy.setToolTipText("復制 (Ctrl+C)");
toolcopy.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
text.copy(); //調用text的copy()方實現復制
}
});
JButton toolpaste = new JButton(Iconpaste);
toolpaste.setToolTipText("粘貼 (Ctrl+V)");
toolpaste.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
text.paste(); //調用text的paste()方法實現粘貼
}
});
JButton toolcut = new JButton(Iconcut);
toolcut.setToolTipText("剪切 (Ctrl+X)");
toolcut.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
text.cut(); //調用text的cut()方法實現剪切
}
});
JButton tooldelete = new JButton(Icondelete);
tooldelete.setToolTipText("刪除 (Ctrl+D)");
tooldelete.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
delete(); //調用 delete() 刪除選定文本
}
});
JButton toolundo = new JButton(Iconundo);
toolundo.setToolTipText("撤消 (Ctrl+Z)");
toolundo.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
setundo(); //調用 setundo() 進行查找
}
});
JButton toolexit = new JButton(Iconexit);
toolexit.setToolTipText("退出 (Alt+F4)");
toolexit.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
exit(); //退出程序
}
});
JButton toolabout = new JButton(Iconabout);
toolabout.setToolTipText("關于 (F1)");
toolabout.addActionListener(new ActionListener() { //加入事件監聽
public void actionPerformed(ActionEvent e) {
about(); //調用 find() 顯示關于對話框
}
});
//把文件菜單中的各個子菜單加入到文件菜單中
File.add(New);
File.add(Open);
File.addSeparator(); //加入菜單分隔條
File.add(Save);
File.add(SaveAs);
File.addSeparator(); //加入菜單分隔條
File.add(PageSetup);
File.add(Print);
File.addSeparator(); //加入菜單分隔條
File.add(Exit);
//把編輯菜單中的各個子菜單加入到編輯菜單中
Edit.add(Undo);
Edit.addSeparator();
Edit.add(Copy);
Edit.add(Paste);
Edit.add(Cut);
Edit.addSeparator(); //加入菜單分隔條
Edit.add(SelectAll);
Edit.add(Delete);
//把查找菜單中的各個子菜單加入到查找菜單中
Search.add(Find);
Search.add(FindNext);
//把格式菜單中的各個子菜單加入到格式菜單中
Format.add(mFont);
Format.add(fontColor);
Format.add(backColor);
//把幫助菜單中的各個子菜單加入到幫助、菜單中
Help.add(About);
mb.add(File); //把文件菜單加入到菜單條中
mb.add(Edit); //把編輯菜單加入到菜單條中
mb.add(Search); //把查找菜單加入到菜單條中
mb.add(Format); //把格式菜單加入到菜單條中
mb.add(Help); //把幫助菜單加入到菜單條中
toolbar.add(toolprint); //把工具欄圖標加入到工具條中
toolbar.addSeparator(); //加入工具欄分隔條
toolbar.add(toolnew);
toolbar.add(toolopen);
toolbar.add(toolsave);
toolbar.addSeparator();
toolbar.add(toolcopy);
toolbar.add(toolpaste);
toolbar.add(toolcut);
toolbar.add(tooldelete);
toolbar.addSeparator();
toolbar.add(toolundo);
toolbar.addSeparator();
toolbar.add(toolexit);
toolbar.addSeparator();
toolbar.add(toolabout);
//將常用菜單加入到彈出菜單中
pm.add(pUndo);
pm.addSeparator();
pm.add(pCopy);
pm.add(pPaste);
pm.add(pCut);
pm.add(pDelete);
pm.addSeparator();
pm.add(pSelectAll);
text.add(pm);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exit(); //關閉窗口
}
});
f.setJMenuBar(mb); //加入菜單欄
f.getContentPane().add("North",toolbar); //加入工具條
f.getContentPane().add("Center",scroller); //加入文本域
f.getContentPane().add("South", p); //加入狀態欄
f.setVisible(true);
}
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
pm.show((Component)e.getSource(),e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
//以下語句監視文本域的內容是否,改變則使文件狀態為"*"
//表示文件沒有保存
public void removeUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText(" * ");
}
Undo.setEnabled(true);
pUndo.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText(" * ");
}
Undo.setEnabled(true);
pUndo.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText(" * ");
}
Undo.setEnabled(true);
pUndo.setEnabled(true);
}
//利用事件源判斷被點擊的菜單項并調用相應的方法響應菜單事件
public void actionPerformed(ActionEvent e) {
if(e.getSource() == New) {
setnew(); //調用newFile()新建文件
}
else if (e.getSource() == Open){
setopen(); //調用openFile()打開文件
}
else if (e.getSource() == Save){
if(fns !="新文件") {
saveFile();
}
else {
saveNewFile();
}
}
else if (e.getSource() == SaveAs){
saveNewFile();
}
else if (e.getSource() == PageSetup){
prt.printDialog(); //顯示打印設置對話框
}
else if (e.getSource() == Print) {
print(); //調用print()實現打印
}
else if (e.getSource() == Exit) {
exit(); //調用System.exit(0)退出程序
}
else if (e.getSource() == Undo || e.getSource() == pUndo ) {
setundo();
}
else if (e.getSource() == Copy || e.getSource() == pCopy ) {
text.copy(); //調用text.copy()實現復制
}
else if (e.getSource() == Paste || e.getSource() == pPaste) {
text.paste(); //調用text.paste()實現粘貼
}
else if (e.getSource() == Cut || e.getSource() == pCut) {
text.cut(); //調用text.cut()實現剪切
}
else if (e.getSource() == SelectAll || e.getSource() == pSelectAll) {
text.selectAll(); //調用text.selectAll()實現全選
}
else if (e.getSource() == Delete || e.getSource() == pDelete) {
delete(); //調用delete()實現刪除
}
else if (e.getSource() == Find) {
find(); //調用find()實現查找
}
else if (e.getSource() == fontColor) {
fontcolor(); //調用fontcolor() 設置字體顏色
}
else if (e.getSource() == backColor) {
backcolor(); //調用backcolor() 設置背景顏色
}
else if (e.getSource() == About) {
about(); //調用about()顯示關于對話框
}
}
void newFile() { //新建文件
text.setText("");
fns = "新文件";
file = null;
//在窗體中顯示標題和文件名
f.setTitle("夢想年華記事本 - [新文件]");
statusFile.setText("新文件!");
}
//判斷當前打開文件是否是新文件,是否已經保存,并調用newFile()
//實現新建文件
void setnew(){
if(statusFile.getText().endsWith(" * ")) {
int option = JOptionPane.showConfirmDialog(null,"文件 "
+fns+" 文字已經被修改! \n 你是否要保存該文件再新建?","警告!",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(fns == "新文件") {
switch(option) {
case JOptionPane.NO_OPTION :
newFile();
break;
case JOptionPane.YES_OPTION :
saveNewFile();
newFile();
break;
default:
break;
}
}
else {
switch(option) {
case JOptionPane.NO_OPTION :
newFile();
break;
case JOptionPane.YES_OPTION :
saveFile();
newFile();
break;
default:
break;
}
}
}
else {
newFile();
}
}
void openFile() { //打開文件
String s = null;
int rv = fc.showOpenDialog(DreamTimesNotePad.this);
if (rv == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile(); //得到文件路徑和文件名
fns = file.getName();
StringBuffer strPool = new StringBuffer();
BufferedReader br;
try { //拋出異常
br = new BufferedReader(new FileReader(file));
s = br.readLine(); //用循環讀入文本并存入緩存
while(s != null) {
strPool.append(s + "\12"); //
s = br.readLine();
}
br.close(); //關閉文件對象
text.setText(strPool.toString()); //將文本內容置入文本域
statusFile.setText("已打開!");
} catch(Exception e) { e.printStackTrace(); }
//在窗體中顯示標題和文件名
f.setTitle("夢想年華記事本 - [" + file.getName() + "]");
}
}
//判斷當前打開文件是否是新文件,是否已經保存,并調用openFile()
//實現打開
void setopen(){
if(statusFile.getText().endsWith(" * ")) {
int option = JOptionPane.showConfirmDialog(null,"文件 "
+fns+" 文字已經被修改! \n 你是否要保存該文件再打開?","警告!",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(fns == "新文件") {
switch(option) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -