本帖最後由 tonyh 於 2020-3-7 15:15 編輯
使用 JOptionPane 類別下的 showMessageDialog() 方法完成 "關於 MyEditor" 的對應動作。- import java.awt.Font;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- //import javax.swing.JDialog;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.ImageIcon;
- import javax.swing.UIManager;
- import javax.swing.border.BevelBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- public class Ch08 implements ActionListener{
-
- String title="My Editor";
- JFrame fm;
- JScrollPane sp;
- JTextArea ta;
- ImageIcon ic,ic_cut,ic_exit,ic_new,ic_open,ic_paste,ic_save,ic_copy,ic_selectall,ic_about,ic_developer;
- JMenuBar mb;
- JMenu mn_file,mn_edit,mn_help;
- JMenuItem mi_new,mi_open,mi_save,mi_exit,mi_copy,mi_paste,mi_cut,mi_selectall,mi_about;
- JFileChooser fc;
- FileNameExtensionFilter filter1;
-
- void initialize()
- {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e){}
- filter1=new FileNameExtensionFilter("純文字文件","txt");
-
- fc=new JFileChooser();
- //fc.addChoosableFileFilter(filter1);
- fc.setFileFilter(filter1);
-
- ic=new ImageIcon(Ch08.class.getResource("pic/editor.png"));
- ic_cut=new ImageIcon(Ch08.class.getResource("pic/cut.png"));
- ic_exit=new ImageIcon(Ch08.class.getResource("pic/exit.png"));
- ic_new=new ImageIcon(Ch08.class.getResource("pic/new.png"));
- ic_open=new ImageIcon(Ch08.class.getResource("pic/open.png"));
- ic_paste=new ImageIcon(Ch08.class.getResource("pic/paste.png"));
- ic_save=new ImageIcon(Ch08.class.getResource("pic/save.png"));
- ic_copy=new ImageIcon(Ch08.class.getResource("pic/copy.png"));
- ic_selectall=new ImageIcon(Ch08.class.getResource("pic/selectall.png"));
- ic_about=new ImageIcon(Ch08.class.getResource("pic/about.png"));
- ic_developer=new ImageIcon(Ch08.class.getResource("pic/developer.png"));
-
- mi_new=new JMenuItem("開新檔案",ic_new);
- mi_new.addActionListener(this);
- mi_open=new JMenuItem("開啟舊檔",ic_open);
- mi_open.addActionListener(this);
- mi_save=new JMenuItem("儲存檔案",ic_save);
- mi_save.addActionListener(this);
- mi_exit=new JMenuItem("結束",ic_exit);
- mi_exit.addActionListener(this);
- mi_copy=new JMenuItem("複製",ic_copy);
- mi_copy.addActionListener(this);
- mi_paste=new JMenuItem("貼上",ic_paste);
- mi_paste.addActionListener(this);
- mi_cut=new JMenuItem("剪下",ic_cut);
- mi_cut.addActionListener(this);
- mi_selectall=new JMenuItem("全選",ic_selectall);
- mi_selectall.addActionListener(this);
- mi_about=new JMenuItem("關於 My Editor",ic_about);
- mi_about.addActionListener(this);
-
- mn_file=new JMenu(" 檔案(F) ");
- mn_file.setMnemonic('F'); //設定快速鍵
- mn_file.add(mi_new);
- mn_file.add(mi_open);
- mn_file.add(mi_save);
- mn_file.addSeparator(); //分隔線
- mn_file.add(mi_exit);
-
- mn_edit=new JMenu(" 編輯(E) ");
- mn_edit.setMnemonic('E'); //設定快速鍵
- mn_edit.add(mi_copy);
- mn_edit.add(mi_paste);
- mn_edit.add(mi_cut);
- mn_edit.addSeparator();
- mn_edit.add(mi_selectall);
-
- mn_help=new JMenu(" 說明(H) ");
- mn_help.setMnemonic('H'); //設定快速鍵
- mn_help.add(mi_about);
-
- mb=new JMenuBar();
- mb.setBorder(new BevelBorder(BevelBorder.RAISED)); //設定具陰影效果的邊框
- mb.add(mn_file);
- mb.add(mn_edit);
- mb.add(mn_help);
-
- ta=new JTextArea();
- ta.setFont(new Font("新細明體", Font.PLAIN, 18));
- ta.setLineWrap(true); //自動換行
-
- sp=new JScrollPane(ta);
-
- fm=new JFrame(title+" - 未命名");
- fm.setBounds(100, 100, 500, 350);
- fm.setIconImage(ic.getImage());
- fm.setVisible(true);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(sp);
- fm.setJMenuBar(mb);
- }
-
- Ch08()
- {
- initialize();
- }
-
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==mi_new)
- {
- ta.setText("");
- fm.setTitle(title+" - 未命名");
- }
- else if(e.getSource()==mi_open)
- {
- int ret=fc.showOpenDialog(null);
- if(ret==JFileChooser.APPROVE_OPTION)
- {
- try
- {
- String str;
- File fi=fc.getSelectedFile();
- BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
- ta.setText(br.readLine());
- do
- {
- str=br.readLine();
- if(str==null)
- break;
- ta.append("\n"+str);
- }while(true);
- br.close();
- fm.setTitle(title+" - "+fc.getName(fi));
- }catch(Exception ex){}
-
- }
- }
- else if(e.getSource()==mi_save)
- {
- int ret=fc.showSaveDialog(null);
- if(ret==JFileChooser.APPROVE_OPTION)
- {
- try
- {
- File fi=fc.getSelectedFile();
- BufferedWriter bw;
- String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4); //file extension 副檔名
- System.out.println(ext);
- String fiPath="";
- if(fc.getFileFilter()==filter1)
- {
- if(ext.equals(".txt"))
- fiPath=fi.getAbsolutePath();
- else
- fiPath=fi.getAbsolutePath()+".txt";
- }
- else
- fiPath=fi.getAbsolutePath();
- bw=new BufferedWriter(new FileWriter(fiPath));
-
- bw.write(ta.getText().replaceAll("\n", "\r\n"));
- //windows下的文字檔分行符號:\r\n linux/unix下的文字檔分行符號:\r Mac下的文字檔分行符號:\n
- bw.flush();
- bw.close();
- fm.setTitle(title+" - "+fc.getName(fi));
- }catch(Exception ex){}
- }
- }
- else if(e.getSource()==mi_exit)
- System.exit(0);
- else if(e.getSource()==mi_copy)
- ta.copy();
- else if(e.getSource()==mi_paste)
- ta.paste();
- else if(e.getSource()==mi_cut)
- ta.cut();
- else if(e.getSource()==mi_selectall)
- ta.selectAll();
- else if(e.getSource()==mi_about)
- {
- String msg="本軟體由社團法人高雄市資訊培育協會青少年程式設計班學員\n林宇翔所開發,感謝您的使用!";
- JOptionPane.showMessageDialog(fm,msg,"關於 My Editor",JOptionPane.INFORMATION_MESSAGE,ic_developer);
- }
- }
-
- public static void main(String[] args) {
- new Ch08();
- }
- }
複製代碼
|