- /*
- awt 类////类似记事本的小程序
- */
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- class Demo002
- {
- public static void main(String[] args)
- {
- new app();
- }
- }
- class app_2
- {
- private Frame f;
- private TextArea ta;
- private Button dlBut;
- private Label dlLabel;
- private MenuBar mb;
- private Menu m;
- private MenuItem closeM,openM,saveM,newM;
- private FileDialog openD,saveD;
- private File saveF,openF;
- app_2()
- {
- init();
- }
- private void init()//初始化
- {
- f = new Frame("优化代码");
- ta = new TextArea("",30,70);
-
- //按钮初始化
- mb = new MenuBar();
- m = new Menu("文件");
- newM = new MenuItem("新建");
- saveM = new MenuItem("保存");
- openM = new MenuItem("打开");
- closeM = new MenuItem("关闭");
-
- //对话框初始化
- openD = new FileDialog(f,"打开文件");
- saveD = new FileDialog(f,"保存文件",FileDialog.SAVE);
-
- //窗体规格设置
- f.setBounds(300,300,600,400);
- f.setVisible(true);
- //按钮添加
- mb.add(m);
- m.add(newM);
- m.add(openM);
- m.add(saveM);
- m.add(closeM);
- //窗体添加组件
- f.add(ta);
- f.setMenuBar(mb);
-
- //调用监听器
- frameListener();
- }
- //主窗体监听事件
- private void frameListener()
- {
- //点击x退出程序
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- closeM.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- //新建菜单
- newM.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- saveF = null;
- openF = null;
- ta.setText("");
- }
- });
- //打开文件对话框 读取文件操作
- openM.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- openD.setVisible(true);
- String dir = openD.getDirectory();
- String path = openD.getFile();
- if(path!=null && dir!=null )
- {
- openF = new File(dir,path);
- ta.setText("");
- BufferedReader buffr = null;
- try
- {
- buffr = new BufferedReader(new FileReader(openF));
- String str = null;
- while((str=buffr.readLine())!=null)
- ta.append(str+"\r\n");
- saveF = openF;//这里是打开文件保存到原来路径的设置
- }
- catch (IOException ex)
- {
- throw new RuntimeException("读取失败的");
- }
- finally
- {
- try
- {
- if(buffr!=null)
- buffr.close();
- }
- catch (IOException ex)
- {
- throw new RuntimeException("关闭失败的");
- }
- }
-
- }
- }
- });
-
- //保存文件对话框 存储文件操作
- saveM.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
-
- if(saveF==null)
- {
- saveD.setVisible(true);
- String dir = saveD.getDirectory();
- String path = saveD.getFile();
- if(path==null || dir==null)
- return ;
- saveF = new File(dir,path);
- }
-
-
-
- BufferedWriter buffw = null;
- try
- {
- buffw = new BufferedWriter(new FileWriter(saveF));
- String text = ta.getText();
- buffw.write(text);
- buffw.flush();
- }
- catch (IOException ex)
- {
- throw new RuntimeException("关闭失败的");
- }
- finally
- {
- try
- {
- if(buffw!=null)
- buffw.close();
- }
- catch (IOException ex)
- {
- throw new RuntimeException("关闭失败的");
- }
- }
- }
- });
- }
- }
复制代码 |
|