記事本程序展示結果圖:
1.建立了一個類Notepad ?extends JFrame ?implements ActionListener , DocumentListener.
DocumentListener
interface for an observe to register to receive a notifications(n. 通知) of changes to a text document.
收到一個文本文檔更改的通知的接口。
asynchronous mutations 異步的突變(變化)
程序設置是從上到下的編寫實現。
(1) JMenu filename,。。。。; 定義好主菜單欄的名字。
(2)JPopupMenu 此處是定義右擊彈出式菜單。 本意是一個彈出式菜單,當用戶選擇一個項目是,可以設置;也可以是 the user right-clicks(pull-right 右拉式) in a specified area.(在指定的區域進行右擊式彈出菜單)??
separator n .分隔符(分隔線)
并定義了相關的JMenuItem 菜單項,即文件下的“新建”,“打開”這就叫做菜單項。
此處設置了與右擊彈出菜單相關的JMenuItem。
A menu item is essentially (本質上)a button sitting in a list. When the user selects the "button", the action associated with the menu ?item is performed。
(3)定義好菜單欄中各個菜單的JMenuItem(菜單項)。
其中格式和查看還要設置JCheckBoxMenuItem(復選菜單項 -復選框的功能)。如下所示:
□自動換行(復選框 在前面方格 可以選擇或者取消選擇)
(4)定義文本編輯區域JTextArea .
(5)定義JLable 狀態欄標簽
(6)設置系統粘貼板
Toolkit toolkit=Toolkit.getDefaultToolkit();
Clipboard clipBoard=toolkit.getSystemClipboard();
toolkit 是工具包,此類是 Abstract Window Toolkit 的所有實際實現的抽象超類。Toolkit 的子類被用于將各種組件綁定到特定本機工具包實現。
Toolkit是抽象類,所以不能用new Toolkit()實例化對象。
但是Toolkit有靜態方法getDefaultToolkit(),通過這個方法可以獲取到Toolkit的對象。
Clipboard 類
A class that implements a mechanism(機制;原理) to transfer data using cut/copy/paste operations.
使用剪切/復制/粘貼操作實現傳輸數據的機制的類。
(7)創建撤銷操作管理器(與撤銷操作有關)
protected UndoManager undo=new UndoManager();
protected UndoableEditListener undoHandler=new UndoHandler();
UndoManager類
UndoManagermanages a list ofUndoableEdits, providing a way to undo or redo the appropriate edits. There are two ways to add edits to anUndoManager. Add the edit directly using theaddEditmethod, or add theUndoManagerto a bean that supportsUndoableEditListener。
(8)其他變量
String oldValue;//存放編輯區原來的內容,用于比較文本是否有改動
boolean isNewFile=true;//是否新文件(未保存過的)
File currentFile;//當前文件名
(9)設置構造函數()-Notepad()-改變文本框的標題和改變系統默認的字體
public Notepad()
{
super("Java記事本");
//改變系統默認字體
Font font = new Font("Dialog", Font.PLAIN, 12);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, font);
}
}
(10)創建菜單條MenuBar
JMenuBar menuBar=new JMenuBar();
(11)創建各個菜單項下的子菜單并注冊事件監聽器
fileMenu=new JMenu("文件(F)");
fileMenu.setMnemonic('F');//設置快捷鍵ALT+F 相當于“新建(N)”括號后的“(N)”。
fileMenu_New=new JMenuItem("新建(N)");
fileMenu_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
fileMenu_New.addActionListener(this); //注冊事件監聽
其他的子菜單項一樣設置,就是換個名字。
設置快捷鍵,實例化變量.setMnemonic('快捷鍵的字母').
加子菜單項用JMenuItem.
setAccelerator相當于“新建(N)“” 后的”Ctrl-N”
(11)創建編輯菜單及菜單項并注冊事件監聽
editMenu=new JMenu("編輯(E)");
editMenu.setMnemonic('E');//設置快捷鍵ALT+E
//當選擇編輯菜單時,設置剪切、復制、粘貼、刪除等功能的可用性
//進行了事件處理。
editMenu.addMenuListener(new MenuListener()
{? public void menuCanceled(MenuEvent e)//取消菜單時調用
? ? ?{?
? ? ? ? checkMenuItemEnabled();//設置剪切、復制、粘貼、刪除等功能的可用性
? ? ?}
public void menuDeselected(MenuEvent e)//取消選擇某個菜單時調用
? ? ? {??
? ? ? ?checkMenuItemEnabled();//設置剪切、復制、粘貼、刪除等功能的可用性
? ?}
public void menuSelected(MenuEvent e)//選擇某個菜單時調用
? ? ?{??
? ? ? ? ?checkMenuItemEnabled();//設置剪切、復制、粘貼、刪除等功能的可用性
? ?}
});
editMenu_Undo=new JMenuItem("撤銷(U)");
editMenu_Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
editMenu_Undo.addActionListener(this);
editMenu_Undo.setEnabled(false);
editMenu_Cut=new JMenuItem("剪切(T)");
editMenu_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
editMenu_Cut.addActionListener(this);
editMenu_Copy=new JMenuItem("復制(C)");
editMenu_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
editMenu_Copy.addActionListener(this);
editMenu_Paste=new JMenuItem("粘貼(P)");
editMenu_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
editMenu_Paste.addActionListener(this);
editMenu_Delete=new JMenuItem("刪除(D)");
editMenu_Delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
editMenu_Delete.addActionListener(this);
(12)把這些菜單項添加到菜單條上add(變量名字)。層層添加,主菜單項添加到菜單條上,子菜單項添加到主菜單項上。
//向菜單條添加"文件"菜單及菜單項
menuBar.add(fileMenu);
fileMenu.add(fileMenu_New);
fileMenu.add(fileMenu_Open);
fileMenu.add(fileMenu_Save);
fileMenu.add(fileMenu_SaveAs);
fileMenu.addSeparator();? ? ? ? //分隔線
fileMenu.add(fileMenu_PageSetUp);
fileMenu.add(fileMenu_Print);
fileMenu.addSeparator();? ? ? ? //分隔線
fileMenu.add(fileMenu_Exit);
//向菜單條添加"編輯"菜單及菜單項
menuBar.add(editMenu);
editMenu.add(editMenu_Undo);
editMenu.addSeparator();? ? ? ? //分隔線
editMenu.add(editMenu_Cut);
editMenu.add(editMenu_Copy);
editMenu.add(editMenu_Paste);
editMenu.add(editMenu_Delete);
editMenu.addSeparator();? ? ? ? //分隔線
editMenu.add(editMenu_Find);
editMenu.add(editMenu_FindNext);
editMenu.add(editMenu_Replace);
editMenu.add(editMenu_GoTo);
editMenu.addSeparator();? ? ? ? //分隔線
editMenu.add(editMenu_SelectAll);
editMenu.add(editMenu_TimeDate);
//向菜單條添加"格式"菜單及菜單項
menuBar.add(formatMenu);
formatMenu.add(formatMenu_LineWrap);
formatMenu.add(formatMenu_Font);
//向菜單條添加"查看"菜單及菜單項
menuBar.add(viewMenu);
viewMenu.add(viewMenu_Status);
//向菜單條添加"幫助"菜單及菜單項
menuBar.add(helpMenu);
helpMenu.add(helpMenu_HelpTopics);
helpMenu.addSeparator();
helpMenu.add(helpMenu_AboutNotepad);
//向窗口添加菜單條
this.setJMenuBar(menuBar);
(13)創建文本編輯區并添加滾動條
editArea=new JTextArea(20,50);
JScrollPane scroller=new JScrollPane(editArea);
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scroller,BorderLayout.CENTER);//向窗口添加文本編輯區
editArea.setWrapStyleWord(true);//設置單詞在一行不足容納時換行
editArea.setLineWrap(true);//設置文本編輯區自動換行默認為true,即會"自動換行"
oldValue=editArea.getText();//獲取原文本編輯區的內容
//編輯區注冊事件監聽(與撤銷操作有關)
editArea.getDocument().addUndoableEditListener(undoHandler);
editArea.getDocument().addDocumentListener(this);
(14)創建右鍵彈出菜單
popupMenu=new JPopupMenu();
popupMenu_Undo=new JMenuItem("撤銷(U)");
popupMenu_Cut=new JMenuItem("剪切(T)");
popupMenu_Copy=new JMenuItem("復制(C)");
popupMenu_Paste=new JMenuItem("粘帖(P)");
popupMenu_Delete=new JMenuItem("刪除(D)");
popupMenu_SelectAll=new JMenuItem("全選(A)");
popupMenu_Undo.setEnabled(false);
//向右鍵菜單添加菜單項和分隔符
popupMenu.add(popupMenu_Undo);
popupMenu.addSeparator();
popupMenu.add(popupMenu_Cut);
popupMenu.add(popupMenu_Copy);
popupMenu.add(popupMenu_Paste);
popupMenu.add(popupMenu_Delete);
popupMenu.addSeparator();
popupMenu.add(popupMenu_SelectAll);
//文本編輯區注冊右鍵菜單事件
popupMenu_Undo.addActionListener(this);
popupMenu_Cut.addActionListener(this);
popupMenu_Copy.addActionListener(this);
popupMenu_Paste.addActionListener(this);
popupMenu_Delete.addActionListener(this);
popupMenu_SelectAll.addActionListener(this);
(15)文本編輯區注冊右鍵菜單事件
editArea.addMouseListener(new MouseAdapter()
{? public void mousePressed(MouseEvent e)
{? if(e.isPopupTrigger())//返回此鼠標事件是否為該平臺的彈出菜單觸發事件
{? popupMenu.show(e.getComponent(),e.getX(),e.getY());//在組件調用者的坐標空間中的位置 X、Y 顯示彈出菜單
}
checkMenuItemEnabled();//設置剪切,復制,粘帖,刪除等功能的可用性
editArea.requestFocus();//編輯區獲取焦點
}
public void mouseReleased(MouseEvent e)
{? if(e.isPopupTrigger())//返回此鼠標事件是否為該平臺的彈出菜單觸發事件
{? popupMenu.show(e.getComponent(),e.getX(),e.getY());//在組件調用者的坐標空間中的位置 X、Y 顯示彈出菜單
}
checkMenuItemEnabled();//設置剪切,復制,粘帖,刪除等功能的可用性
editArea.requestFocus();//編輯區獲取焦點
}
});//文本編輯區注冊右鍵菜單事件結束
(16)//創建和添加狀態欄
statusLabel=new JLabel(" 按F1獲取幫助");
this.add(statusLabel,BorderLayout.SOUTH);//向窗口添加狀態欄標簽
//設置窗口在屏幕上的位置、大小和可見性
this.setLocation(100,100);
this.setSize(650,550);
this.setVisible(true);
//添加窗口監聽器
addWindowListener(new WindowAdapter()
{? public void windowClosing(WindowEvent e)
{? exitWindowChoose();
}
});
checkMenuItemEnabled();? //設置剪切,復制,粘帖,刪除等功能的可用性
editArea.requestFocus();? //編輯區獲取焦點
}//構造函數Notepad結束
(17)//設置菜單項的可用性:剪切,復制,粘帖,刪除功能
public void checkMenuItemEnabled()
{? String selectText=editArea.getSelectedText();
if(selectText==null)
{? editMenu_Cut.setEnabled(false);
popupMenu_Cut.setEnabled(false);
editMenu_Copy.setEnabled(false);
popupMenu_Copy.setEnabled(false);
editMenu_Delete.setEnabled(false);
popupMenu_Delete.setEnabled(false);
}
else
{? editMenu_Cut.setEnabled(true);
popupMenu_Cut.setEnabled(true);
editMenu_Copy.setEnabled(true);
popupMenu_Copy.setEnabled(true);
editMenu_Delete.setEnabled(true);
popupMenu_Delete.setEnabled(true);
}
//粘帖功能可用性判斷
Transferable contents=clipBoard.getContents(this);
if(contents==null)
{? editMenu_Paste.setEnabled(false);
popupMenu_Paste.setEnabled(false);
}
else
{? editMenu_Paste.setEnabled(true);
popupMenu_Paste.setEnabled(true);
}
}//方法checkMenuItemEnabled()結束
(18)//關閉窗口時調用
public void exitWindowChoose()
{? editArea.requestFocus();
String currentValue=editArea.getText();
if(currentValue.equals(oldValue)==true)
{? System.exit(0);
}
else
{? int exitChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","退出提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(exitChoose==JOptionPane.YES_OPTION)
{? //boolean isSave=false;
if(isNewFile)
{
String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("另存為");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText(" 您沒有保存文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null||saveFileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();
fw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+"? - 記事本");
statusLabel.setText(" 當前打開文件:"+saveFileName.getAbsoluteFile());
//isSave=true;
}
catch(IOException ioException){
}
}
}
else
{
try
{? FileWriter fw=new FileWriter(currentFile);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();
fw.close();
//isSave=true;
}
catch(IOException ioException){
}
}
System.exit(0);
//if(isSave)System.exit(0);
//else return;
}
else if(exitChoose==JOptionPane.NO_OPTION)
{? System.exit(0);
}
else
{? return;
}
}
}//關閉窗口時調用方法結束
JOptionPane(對話框)
showConfirmDialog:Asks a confirming question, like yes/no/cancel.
int exitChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","退出提示",JOptionPane.YES_NO_CANCEL_OPTION);
圖中的退出提示就是JOptionPane.showConfirmDialog的用法展示。
JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE。
showMessageDialog :Tell the user about something that has happened.
JFileChooser(文件選擇對話框)
JFileChooserprovides a simple mechanism(機制;原理) for the user to choose a file.
彈出一個對話框選擇文件。
圖中的中間的“打開文件這樣的文件選擇框”。
(19)子菜單項“查找”“替換”“全部替換”“字體”的設置方法和事件處理
//查找方法
public void find()
{? final JDialog findDialog=new JDialog(this,"查找",false);//false時允許其他窗口同時處于激活狀態(即無模式)
Container con=findDialog.getContentPane();//返回此對話框的contentPane對象
con.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel findContentLabel=new JLabel("查找內容(N):");
final JTextField findText=new JTextField(15);
JButton findNextButton=new JButton("查找下一個(F):");
final JCheckBox matchCheckBox=new JCheckBox("區分大小寫(C)");
ButtonGroup bGroup=new ButtonGroup();
final JRadioButton upButton=new JRadioButton("向上(U)");
final JRadioButton downButton=new JRadioButton("向下(U)");
downButton.setSelected(true);
bGroup.add(upButton);
bGroup.add(downButton);
/*ButtonGroup此類用于為一組按鈕創建一個多斥(multiple-exclusion)作用域。
使用相同的 ButtonGroup 對象創建一組按鈕意味著“開啟”其中一個按鈕時,將關閉組中的其他所有按鈕。*/
/*JRadioButton此類實現一個單選按鈕,此按鈕項可被選擇或取消選擇,并可為用戶顯示其狀態。
與 ButtonGroup 對象配合使用可創建一組按鈕,一次只能選擇其中的一個按鈕。
(創建一個 ButtonGroup 對象并用其 add 方法將 JRadioButton 對象包含在此組中。)*/
JButton cancel=new JButton("取消");
//取消按鈕事件處理
cancel.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? findDialog.dispose();
}
});
//"查找下一個"按鈕監聽
findNextButton.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? //"區分大小寫(C)"的JCheckBox是否被選中
int k=0,m=0;
final String str1,str2,str3,str4,strA,strB;
str1=editArea.getText();
str2=findText.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//區分大小寫
{? strA=str1;
strB=str2;
}
else//不區分大小寫,此時把所選內容全部化成大寫(或小寫),以便于查找
{? strA=str3;
strB=str4;
}
if(upButton.isSelected())
{? //k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if(editArea.getSelectedText()==null)
k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? JOptionPane.showMessageDialog(null,"找不到您查找的內容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(downButton.isSelected())
{? if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1);
else
k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? JOptionPane.showMessageDialog(null,"找不到您查找的內容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
}
});//"查找下一個"按鈕監聽結束
//創建"查找"對話框的界面
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel directionPanel=new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
//設置directionPanel組件的邊框;
//BorderFactory.createTitledBorder(String title)創建一個新標題邊框,使用默認邊框(浮雕化)、默認文本位置(位于頂線上)、默認調整 (leading) 以及由當前外觀確定的默認字體和文本顏色,并指定了標題文本。
directionPanel.add(upButton);
directionPanel.add(downButton);
panel1.setLayout(new GridLayout(2,1));
panel1.add(findNextButton);
panel1.add(cancel);
panel2.add(findContentLabel);
panel2.add(findText);
panel2.add(panel1);
panel3.add(matchCheckBox);
panel3.add(directionPanel);
con.add(panel2);
con.add(panel3);
findDialog.setSize(410,180);
findDialog.setResizable(false);//不可調整大小
findDialog.setLocation(230,280);
findDialog.setVisible(true);
}//查找方法結束
//替換方法
public void replace()
{? final JDialog replaceDialog=new JDialog(this,"替換",false);//false時允許其他窗口同時處于激活狀態(即無模式)
Container con=replaceDialog.getContentPane();//返回此對話框的contentPane對象
con.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel findContentLabel=new JLabel("查找內容(N):");
final JTextField findText=new JTextField(15);
JButton findNextButton=new JButton("查找下一個(F):");
JLabel replaceLabel=new JLabel("替換為(P):");
final JTextField replaceText=new JTextField(15);
JButton replaceButton=new JButton("替換(R)");
JButton replaceAllButton=new JButton("全部替換(A)");
JButton cancel=new JButton("取消");
cancel.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? replaceDialog.dispose();
}
});
final JCheckBox matchCheckBox=new JCheckBox("區分大小寫(C)");
ButtonGroup bGroup=new ButtonGroup();
final JRadioButton upButton=new JRadioButton("向上(U)");
final JRadioButton downButton=new JRadioButton("向下(U)");
downButton.setSelected(true);
bGroup.add(upButton);
bGroup.add(downButton);
/*ButtonGroup此類用于為一組按鈕創建一個多斥(multiple-exclusion)作用域。
使用相同的 ButtonGroup 對象創建一組按鈕意味著“開啟”其中一個按鈕時,將關閉組中的其他所有按鈕。*/
/*JRadioButton此類實現一個單選按鈕,此按鈕項可被選擇或取消選擇,并可為用戶顯示其狀態。
與 ButtonGroup 對象配合使用可創建一組按鈕,一次只能選擇其中的一個按鈕。
(創建一個 ButtonGroup 對象并用其 add 方法將 JRadioButton 對象包含在此組中。)*/
//"查找下一個"按鈕監聽
findNextButton.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? //"區分大小寫(C)"的JCheckBox是否被選中
int k=0,m=0;
final String str1,str2,str3,str4,strA,strB;
str1=editArea.getText();
str2=findText.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//區分大小寫
{? strA=str1;
strB=str2;
}
else//不區分大小寫,此時把所選內容全部化成大寫(或小寫),以便于查找
{? strA=str3;
strB=str4;
}
if(upButton.isSelected())
{? //k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if(editArea.getSelectedText()==null)
k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? JOptionPane.showMessageDialog(null,"找不到您查找的內容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
else if(downButton.isSelected())
{? if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1);
else
k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? JOptionPane.showMessageDialog(null,"找不到您查找的內容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
}
});//"查找下一個"按鈕監聽結束
//"替換"按鈕監聽
replaceButton.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? if(replaceText.getText().length()==0 && editArea.getSelectedText()!=null)
editArea.replaceSelection("");
if(replaceText.getText().length()>0 && editArea.getSelectedText()!=null)
editArea.replaceSelection(replaceText.getText());
}
});//"替換"按鈕監聽結束
//"全部替換"按鈕監聽
replaceAllButton.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? editArea.setCaretPosition(0);? //將光標放到編輯區開頭
int k=0,m=0,replaceCount=0;
if(findText.getText().length()==0)
{? JOptionPane.showMessageDialog(replaceDialog,"請填寫查找內容!","提示",JOptionPane.WARNING_MESSAGE);
findText.requestFocus(true);
return;
}
while(k>-1)//當文本中有內容被選中時(k>-1被選中)進行替換,否則不進行while循環
{? //"區分大小寫(C)"的JCheckBox是否被選中
//int k=0,m=0;
final String str1,str2,str3,str4,strA,strB;
str1=editArea.getText();
str2=findText.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(matchCheckBox.isSelected())//區分大小寫
{? strA=str1;
strB=str2;
}
else//不區分大小寫,此時把所選內容全部化成大寫(或小寫),以便于查找
{? strA=str3;
strB=str4;
}
if(upButton.isSelected())
{? //k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
if(editArea.getSelectedText()==null)
k=strA.lastIndexOf(strB,editArea.getCaretPosition()-1);
else
k=strA.lastIndexOf(strB, editArea.getCaretPosition()-findText.getText().length()-1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? if(replaceCount==0)
{? JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的內容!", "記事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{? JOptionPane.showMessageDialog(replaceDialog,"成功替換"+replaceCount+"個匹配內容!","替換成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(downButton.isSelected())
{? if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1);
else
k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{? //String strData=strA.subString(k,strB.getText().length()+1);
editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{? if(replaceCount==0)
{? JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的內容!", "記事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{? JOptionPane.showMessageDialog(replaceDialog,"成功替換"+replaceCount+"個匹配內容!","替換成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(replaceText.getText().length()==0 && editArea.getSelectedText()!= null)
{? editArea.replaceSelection("");
replaceCount++;
}
if(replaceText.getText().length()>0 && editArea.getSelectedText()!= null)
{? editArea.replaceSelection(replaceText.getText());
replaceCount++;
}
}//while循環結束
}
});//"替換全部"方法結束
//創建"替換"對話框的界面
JPanel directionPanel=new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
//設置directionPanel組件的邊框;
//BorderFactory.createTitledBorder(String title)創建一個新標題邊框,使用默認邊框(浮雕化)、默認文本位置(位于頂線上)、默認調整 (leading) 以及由當前外觀確定的默認字體和文本顏色,并指定了標題文本。
directionPanel.add(upButton);
directionPanel.add(downButton);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(2,1));
panel1.add(findContentLabel);
panel1.add(findText);
panel1.add(findNextButton);
panel4.add(replaceButton);
panel4.add(replaceAllButton);
panel2.add(replaceLabel);
panel2.add(replaceText);
panel2.add(panel4);
panel3.add(matchCheckBox);
panel3.add(directionPanel);
panel3.add(cancel);
con.add(panel1);
con.add(panel2);
con.add(panel3);
replaceDialog.setSize(420,220);
replaceDialog.setResizable(false);//不可調整大小
replaceDialog.setLocation(230,280);
replaceDialog.setVisible(true);
}//"全部替換"按鈕監聽結束
//"字體"方法
public void font()
{? final JDialog fontDialog=new JDialog(this,"字體設置",false);
Container con=fontDialog.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel fontLabel=new JLabel("字體(F):");
fontLabel.setPreferredSize(new Dimension(100,20));//構造一個Dimension,并將其初始化為指定寬度和高度
JLabel styleLabel=new JLabel("字形(Y):");
styleLabel.setPreferredSize(new Dimension(100,20));
JLabel sizeLabel=new JLabel("大小(S):");
sizeLabel.setPreferredSize(new Dimension(100,20));
final JLabel sample=new JLabel("張選仲的記事本-ZXZ's Notepad");
//sample.setHorizontalAlignment(SwingConstants.CENTER);
final JTextField fontText=new JTextField(9);
fontText.setPreferredSize(new Dimension(200,20));
final JTextField styleText=new JTextField(8);
styleText.setPreferredSize(new Dimension(200,20));
final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
final JTextField sizeText=new JTextField(5);
sizeText.setPreferredSize(new Dimension(200,20));
JButton okButton=new JButton("確定");
JButton cancel=new JButton("取消");
cancel.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? fontDialog.dispose();
}
});
Font currentFont=editArea.getFont();
fontText.setText(currentFont.getFontName());
fontText.selectAll();
//styleText.setText(currentFont.getStyle());
//styleText.selectAll();
if(currentFont.getStyle()==Font.PLAIN)
styleText.setText("常規");
else if(currentFont.getStyle()==Font.BOLD)
styleText.setText("粗體");
else if(currentFont.getStyle()==Font.ITALIC)
styleText.setText("斜體");
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
styleText.setText("粗斜體");
styleText.selectAll();
String str=String.valueOf(currentFont.getSize());
sizeText.setText(str);
sizeText.selectAll();
final JList fontList,styleList,sizeList;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
final String fontName[]=ge.getAvailableFontFamilyNames();
fontList=new JList(fontName);
fontList.setFixedCellWidth(86);
fontList.setFixedCellHeight(20);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final String fontStyle[]={"常規","粗體","斜體","粗斜體"};
styleList=new JList(fontStyle);
styleList.setFixedCellWidth(86);
styleList.setFixedCellHeight(20);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if(currentFont.getStyle()==Font.PLAIN)
styleList.setSelectedIndex(0);
else if(currentFont.getStyle()==Font.BOLD)
styleList.setSelectedIndex(1);
else if(currentFont.getStyle()==Font.ITALIC)
styleList.setSelectedIndex(2);
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
styleList.setSelectedIndex(3);
final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
sizeList=new JList(fontSize);
sizeList.setFixedCellWidth(43);
sizeList.setFixedCellHeight(20);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.addListSelectionListener(new ListSelectionListener()
{? public void valueChanged(ListSelectionEvent event)
{? fontText.setText(fontName[fontList.getSelectedIndex()]);
fontText.selectAll();
Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont1);
}
});
styleList.addListSelectionListener(new ListSelectionListener()
{? public void valueChanged(ListSelectionEvent event)
{? int s=style[styleList.getSelectedIndex()];
styleText.setText(fontStyle[s]);
styleText.selectAll();
Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont2);
}
});
sizeList.addListSelectionListener(new ListSelectionListener()
{? public void valueChanged(ListSelectionEvent event)
{? sizeText.setText(fontSize[sizeList.getSelectedIndex()]);
//sizeText.requestFocus();
sizeText.selectAll();
Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont3);
}
});
okButton.addActionListener(new ActionListener()
{? public void actionPerformed(ActionEvent e)
{? Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
editArea.setFont(okFont);
fontDialog.dispose();
}
});
JPanel samplePanel=new JPanel();
samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
//samplePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
samplePanel.add(sample);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel2.add(fontText);
panel2.add(styleText);
panel2.add(sizeText);
panel2.add(okButton);
panel3.add(new JScrollPane(fontList));//JList不支持直接滾動,所以要讓JList作為JScrollPane的視口視圖
panel3.add(new JScrollPane(styleList));
panel3.add(new JScrollPane(sizeList));
panel3.add(cancel);
con.add(panel1);
con.add(panel2);
con.add(panel3);
con.add(samplePanel);
fontDialog.setSize(350,340);
fontDialog.setLocation(200,200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
(19)第一個“文件”的子菜單項的事件處理
public void actionPerformed(ActionEvent e)
{? //新建
if(e.getSource()==fileMenu_New)
{? editArea.requestFocus();
String currentValue=editArea.getText();
boolean isTextChange=(currentValue.equals(oldValue))?false:true;
if(isTextChange)
{? int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(saveChoose==JOptionPane.YES_OPTION)
{? String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("另存為");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText("您沒有選擇任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null || saveFileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();//刷新該流的緩沖
bfw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+" - 記事本");
statusLabel.setText("當前打開文件:"+saveFileName.getAbsoluteFile());
}
catch (IOException ioException)
{
}
}
}
else if(saveChoose==JOptionPane.NO_OPTION)
{? editArea.replaceRange("",0,editArea.getText().length());
statusLabel.setText(" 新建文件");
this.setTitle("無標題 - 記事本");
isNewFile=true;
undo.discardAllEdits(); //撤消所有的"撤消"操作
editMenu_Undo.setEnabled(false);
oldValue=editArea.getText();
}
else if(saveChoose==JOptionPane.CANCEL_OPTION)
{? return;
}
}
else
{? editArea.replaceRange("",0,editArea.getText().length());
statusLabel.setText(" 新建文件");
this.setTitle("無標題 - 記事本");
isNewFile=true;
undo.discardAllEdits();//撤消所有的"撤消"操作
editMenu_Undo.setEnabled(false);
oldValue=editArea.getText();
}
}//新建結束
//打開
else if(e.getSource()==fileMenu_Open)
{? editArea.requestFocus();
String currentValue=editArea.getText();
boolean isTextChange=(currentValue.equals(oldValue))?false:true;
if(isTextChange)
{? int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(saveChoose==JOptionPane.YES_OPTION)
{? String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("另存為");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText("您沒有選擇任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null || saveFileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();//刷新該流的緩沖
bfw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+" - 記事本");
statusLabel.setText("當前打開文件:"+saveFileName.getAbsoluteFile());
}
catch (IOException ioException)
{
}
}
}
else if(saveChoose==JOptionPane.NO_OPTION)
{? String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("打開文件");
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText("您沒有選擇任何文件");
return;
}
File fileName=fileChooser.getSelectedFile();
if(fileName==null || fileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileReader fr=new FileReader(fileName);
BufferedReader bfr=new BufferedReader(fr);
editArea.setText("");
while((str=bfr.readLine())!=null)
{? editArea.append(str);
}
this.setTitle(fileName.getName()+" - 記事本");
statusLabel.setText(" 當前打開文件:"+fileName.getAbsoluteFile());
fr.close();
isNewFile=false;
currentFile=fileName;
oldValue=editArea.getText();
}
catch (IOException ioException)
{
}
}
}
else
{? return;
}
}
else
{? String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("打開文件");
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText(" 您沒有選擇任何文件 ");
return;
}
File fileName=fileChooser.getSelectedFile();
if(fileName==null || fileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileReader fr=new FileReader(fileName);
BufferedReader bfr=new BufferedReader(fr);
editArea.setText("");
while((str=bfr.readLine())!=null)
{? editArea.append(str);
}
this.setTitle(fileName.getName()+" - 記事本");
statusLabel.setText(" 當前打開文件:"+fileName.getAbsoluteFile());
fr.close();
isNewFile=false;
currentFile=fileName;
oldValue=editArea.getText();
}
catch (IOException ioException)
{
}
}
}
}//打開結束
//保存
else if(e.getSource()==fileMenu_Save)
{? editArea.requestFocus();
if(isNewFile)
{? String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("保存");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText("您沒有選擇任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null || saveFileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();//刷新該流的緩沖
bfw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+" - 記事本");
statusLabel.setText("當前打開文件:"+saveFileName.getAbsoluteFile());
}
catch (IOException ioException)
{
}
}
}
else
{? try
{? FileWriter fw=new FileWriter(currentFile);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();
fw.close();
}
catch(IOException ioException)
{
}
}
}//保存結束
//另存為
else if(e.getSource()==fileMenu_SaveAs)
{? editArea.requestFocus();
String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fileChooser.setApproveButtonText("確定");
fileChooser.setDialogTitle("另存為");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{? statusLabel.setText(" 您沒有選擇任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null||saveFileName.getName().equals(""))
{? JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{? try
{? FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();
fw.close();
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+"? - 記事本");
statusLabel.setText(" 當前打開文件:"+saveFileName.getAbsoluteFile());
}
catch(IOException ioException)
{
}
}
}//另存為結束
//頁面設置
else if(e.getSource()==fileMenu_PageSetUp)
{? editArea.requestFocus();
JOptionPane.showMessageDialog(this,"對不起,此功能尚未實現!","提示",JOptionPane.WARNING_MESSAGE);
}//頁面設置結束
//打印
else if(e.getSource()==fileMenu_Print)
{? editArea.requestFocus();
JOptionPane.showMessageDialog(this,"對不起,此功能尚未實現!","提示",JOptionPane.WARNING_MESSAGE);
}//打印結束
//退出
else if(e.getSource()==fileMenu_Exit)
{? int exitChoose=JOptionPane.showConfirmDialog(this,"確定要退出嗎?","退出提示",JOptionPane.OK_CANCEL_OPTION);
if(exitChoose==JOptionPane.OK_OPTION)
{? System.exit(0);
}
else
{? return;
}
}//退出結束
//編輯
//else if(e.getSource()==editMenu)
//{ checkMenuItemEnabled();//設置剪切、復制、粘貼、刪除等功能的可用性
//}
//編輯結束
//撤銷
else if(e.getSource()==editMenu_Undo || e.getSource()==popupMenu_Undo)
{? editArea.requestFocus();
if(undo.canUndo())
{? try
{? undo.undo();
}
catch (CannotUndoException ex)
{? System.out.println("Unable to undo:" + ex);
//ex.printStackTrace();
}
}
if(!undo.canUndo())
{? editMenu_Undo.setEnabled(false);
}
}//撤銷結束
//剪切
else if(e.getSource()==editMenu_Cut || e.getSource()==popupMenu_Cut)
{? editArea.requestFocus();
String text=editArea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
checkMenuItemEnabled();//設置剪切,復制,粘帖,刪除功能的可用性
}//剪切結束
//復制
else if(e.getSource()==editMenu_Copy || e.getSource()==popupMenu_Copy)
{? editArea.requestFocus();
String text=editArea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
checkMenuItemEnabled();//設置剪切,復制,粘帖,刪除功能的可用性
}//復制結束
//粘帖
else if(e.getSource()==editMenu_Paste || e.getSource()==popupMenu_Paste)
{? editArea.requestFocus();
Transferable contents=clipBoard.getContents(this);
if(contents==null)return;
String text="";
try
{? text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception exception)
{
}
editArea.replaceRange(text,editArea.getSelectionStart(),editArea.getSelectionEnd());
checkMenuItemEnabled();
}//粘帖結束
//刪除
else if(e.getSource()==editMenu_Delete || e.getSource()==popupMenu_Delete)
{? editArea.requestFocus();
editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
checkMenuItemEnabled(); //設置剪切、復制、粘貼、刪除等功能的可用性
}//刪除結束
//查找
else if(e.getSource()==editMenu_Find)
{? editArea.requestFocus();
find();
}//查找結束
//查找下一個
else if(e.getSource()==editMenu_FindNext)
{? editArea.requestFocus();
find();
}//查找下一個結束
//替換
else if(e.getSource()==editMenu_Replace)
{? editArea.requestFocus();
replace();
}//替換結束
//轉到
else if(e.getSource()==editMenu_GoTo)
{? editArea.requestFocus();
JOptionPane.showMessageDialog(this,"對不起,此功能尚未實現!","提示",JOptionPane.WARNING_MESSAGE);
}//轉到結束
//時間日期
else if(e.getSource()==editMenu_TimeDate)
{? editArea.requestFocus();
//SimpleDateFormat currentDateTime=new SimpleDateFormat("HH:mmyyyy-MM-dd");
//editArea.insert(currentDateTime.format(new Date()),editArea.getCaretPosition());
Calendar rightNow=Calendar.getInstance();
Date date=rightNow.getTime();
editArea.insert(date.toString(),editArea.getCaretPosition());
}//時間日期結束
//全選
else if(e.getSource()==editMenu_SelectAll || e.getSource()==popupMenu_SelectAll)
{? editArea.selectAll();
}//全選結束
//自動換行(已在前面設置)
else if(e.getSource()==formatMenu_LineWrap)
{? if(formatMenu_LineWrap.getState())
editArea.setLineWrap(true);
else
editArea.setLineWrap(false);
}//自動換行結束
//字體設置
else if(e.getSource()==formatMenu_Font)
{? editArea.requestFocus();
font();
}//字體設置結束
//設置狀態欄可見性
else if(e.getSource()==viewMenu_Status)
{? if(viewMenu_Status.getState())
statusLabel.setVisible(true);
else
statusLabel.setVisible(false);
}//設置狀態欄可見性結束
//幫助主題
else if(e.getSource()==helpMenu_HelpTopics)
{? editArea.requestFocus();
JOptionPane.showMessageDialog(this,"用當下的拼搏照亮突破自己的勇氣。","查看幫助",JOptionPane.INFORMATION_MESSAGE);
}//幫助主題結束
//關于
else if(e.getSource()==helpMenu_AboutNotepad)
{? editArea.requestFocus();
JOptionPane.showMessageDialog(this,
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"+
" 編寫者:王海玲 \n"+
" 編寫時間:2017-12-14? ? ? ? ? ? ? ? ? ? ? ? ? \n"+
" 更多問題:可以聯系我的郵箱哦? ? \n"+
" e-mail:123456789@163.com? ? ? ? ? ? ? ? \n"+
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n",
"記事本",JOptionPane.INFORMATION_MESSAGE);
}//關于結束
}//方法actionPerformed()結束
//實現DocumentListener接口中的方法(與撤銷操作有關)
public void removeUpdate(DocumentEvent e)
{? editMenu_Undo.setEnabled(true);
}
public void insertUpdate(DocumentEvent e)
{? editMenu_Undo.setEnabled(true);
}
public void changedUpdate(DocumentEvent e)
{? editMenu_Undo.setEnabled(true);
}//DocumentListener結束
//實現接口UndoableEditListener的類UndoHandler(與撤銷操作有關)
class UndoHandler implements UndoableEditListener
{? public void undoableEditHappened(UndoableEditEvent uee)
{? undo.addEdit(uee.getEdit());
}
}
(20)主函數執行