- import java.awt.Font;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.*;
- import javax.swing.border.BevelBorder;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.BufferedReader;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class Ch09 implements ActionListener{
- JFrame f;
- JTextArea t;
- JScrollPane s;
- JMenuItem mi[]=new JMenuItem[9];
- String name[]=new String[]{"new","open","save","exit","copy","paste","cut","selectall","about"};
- ImageIcon ic,d;
- JMenuBar mb;
- JMenu fl,ed,h,size,font,style,settings;
- JRadioButtonMenuItem rbmi[][]=new JRadioButtonMenuItem[3][];
- ButtonGroup bg[]=new ButtonGroup[3];
- JFileChooser fc;
- FileNameExtensionFilter filter1,filter2;
- String title="MyEditor";
- Ch09()
- {
- d=new ImageIcon(Ch09.class.getResource("pic/developer.png"));
- ic=new ImageIcon(Ch09.class.getResource("pic/editor.png"));
- try
- {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }catch(Exception e){}
- fc=new JFileChooser();
- rbmi[0]=new JRadioButtonMenuItem[3];
- rbmi[1]=new JRadioButtonMenuItem[4];
- rbmi[2]=new JRadioButtonMenuItem[2];
- rbmi[0][0]=new JRadioButtonMenuItem("大(22)");
- rbmi[0][1]=new JRadioButtonMenuItem("中(18)",true);
- rbmi[0][2]=new JRadioButtonMenuItem("小(14)");
- rbmi[1][0]=new JRadioButtonMenuItem("Plain",true);
- rbmi[1][1]=new JRadioButtonMenuItem("Bold");
- rbmi[1][2]=new JRadioButtonMenuItem("Italic");
- rbmi[1][3]=new JRadioButtonMenuItem("Bold + Italic");
- rbmi[2][0]=new JRadioButtonMenuItem("白底黑字",true);
- rbmi[2][1]=new JRadioButtonMenuItem("黑底白字");
- for(int i=0;i<3;i++)
- bg[i]=new ButtonGroup();
- size=new JMenu("size");
- size.setIcon(new ImageIcon(Ch09.class.getResource("pic/size.png")));
- font=new JMenu("font");
- font.setIcon(new ImageIcon(Ch09.class.getResource("pic/type.png")));
- style=new JMenu("style");
- style.setIcon(new ImageIcon(Ch09.class.getResource("pic/style.png")));
- for(int i=0;i<3;i++)
- for(int j=0;j<rbmi[i].length;j++)
- bg[i].add(rbmi[i][j]);
- for(int i=0;i<rbmi[0].length;i++)
- size.add(rbmi[0][i]);
- for(int i=0;i<rbmi[1].length;i++)
- font.add(rbmi[1][i]);
- for(int i=0;i<rbmi[2].length;i++)
- style.add(rbmi[2][i]);
- for(int i=0;i<9;i++)
- {
- mi[i]=new JMenuItem(name[i],new ImageIcon(Ch09.class.getResource("pic/"+name[i]+".png")));
- }
- fl=new JMenu("檔案(F)");
- ed=new JMenu("編輯(E)");
- settings=new JMenu("設定(O)");
- h=new JMenu("說明(H)");
- fl.setMnemonic('F');
- ed.setMnemonic('E');
- settings.setMnemonic('O');
- h.setMnemonic('H');
- settings.add(size);
- settings.add(font);
- settings.add(style);
- filter1=new FileNameExtensionFilter("純文字", "txt");
- filter2=new FileNameExtensionFilter("圖片檔", "jpg","png","bmp");
- fc.addChoosableFileFilter(filter1);
- fc.addChoosableFileFilter(filter2);
- fc.setFileFilter(filter1);
- for(int i=0;i<3;i++)
- {
- mi[i].addActionListener(this);
- fl.add(mi[i]);
- }
- fl.addSeparator();
- mi[3].addActionListener(this);
- fl.add(mi[3]);
- for(int i=4;i<7;i++)
- {
- mi[i].addActionListener(this);
- ed.add(mi[i]);
- }
- mi[8].addActionListener(this);
- h.add(mi[8]);
- ed.addSeparator();
- ed.add(mi[7]);
- mi[7].addActionListener(this);
- mb=new JMenuBar();
- mb.setBorder(new BevelBorder(BevelBorder.RAISED));
- mb.add(fl);
- mb.add(ed);
- mb.add(settings);
- mb.add(h);
- t=new JTextArea();
- t.setLineWrap(true);
- t.setFont(new Font("新細明體",Font.PLAIN,15));
- s=new JScrollPane(t);
- f=new JFrame(title+" 未命名");
- f.setBounds(100,100,400,300);
- f.setIconImage(ic.getImage());
- f.setResizable(true);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.add(s);
- f.setJMenuBar(mb);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==mi[0])
- {
- t.setText("");
- f.setTitle(title+" 未命名");
- }
- else if(e.getSource()==mi[3])
- System.exit(0);
- else if(e.getSource()==mi[4])
- {
- t.copy();
- }
- else if(e.getSource()==mi[5])
- {
- t.paste();
- }
- else if(e.getSource()==mi[6])
- {
- t.cut();
- }
- else if(e.getSource()==mi[7])
- {
- t.selectAll();
- }
- else if(e.getSource()==mi[2])
- {
- int r=fc.showSaveDialog(null);
- File fi=fc.getSelectedFile();
- String fPath=fi.getAbsolutePath();
- String ext=fi.getAbsolutePath().substring(fi.getAbsolutePath().length()-4);
- if(r==JFileChooser.APPROVE_OPTION)
- {
- System.out.println(ext);
- if(fc.getFileFilter()==filter1&&!ext.equals(".txt"))
- fPath+=".txt";
- else if(fc.getFileFilter()==filter2&&!ext.equals(".jpg"))
- fPath+=".jpg";
- try
- {
- BufferedWriter bw=new BufferedWriter(new FileWriter(fPath));
- bw.write(t.getText().replaceAll("\n","\r\n"));
- bw.flush();
- bw.close();
- f.setTitle(title+" - "+fPath);
- }
- catch(Exception ex){}
- }
- }
- else if(e.getSource()==mi[1])
- {
- int r=fc.showOpenDialog(null);
- File fi=fc.getSelectedFile();
- if(r==JFileChooser.APPROVE_OPTION)
- {
- try
- {
- BufferedReader br=new BufferedReader(new FileReader(fi.getAbsolutePath()));
- t.setText(br.readLine());
- while(true)
- {
- String s=br.readLine();
- if(s==null)
- break;
- t.append("\n"+s);
- }
- br.close();
- }
- catch(Exception ex){}
- }
- }
- else if(e.getSource()==mi[8])
- {
- String msg="A";
-
- JOptionPane.showMessageDialog(f,msg,"About",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- public static void main(String[] arg)
- {
- new Ch09();
- }
- }
複製代碼 |