本帖最后由 我叫MT 于 2014-2-4 23:05 编辑
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class AwtDemo3
- {
- private Frame f;
- private TextArea ta;
- private FileDialog openDia,saveDia;
- private MenuBar mb;
- private Menu m,m2,m3,m4;
- private MenuItem mi,mi2,mi3,mi4;
- AwtDemo3()
- {
- init();
- }
- public void init()
- {
- f = new Frame("AWT");
- f.setBounds(200,0,900,600);
- mb =new MenuBar();
- m =new Menu("文件");
- m2 =new Menu("选项");
- m3 =new Menu("设置");
- m4 =new Menu("工具");
- mi =new MenuItem("退出(exit)");
- mi2 =new MenuItem("没工具啦");
- mi3 =new MenuItem("打开(open)");
- mi4 =new MenuItem("保存(save)");
- mb.add(m);
- mb.add(m2);
- mb.add(m3);
- m4.add(mi2);
- m.add(m4);
- m.add(mi3);
- m.add(mi4);
- m.add(mi);
- f.setMenuBar(mb);
- openDia =new FileDialog(f,"打开",FileDialog.LOAD);
- saveDia =new FileDialog(f,"保存",FileDialog.SAVE);
- ta = new TextArea(25,100);
- f.add(ta);
- myEvent();
- f.setVisible(true);
- }
- public void myEvent()
- {
- mi4.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- saveDia.setVisible(true);
- String dirPath = saveDia.getDirectory();
- String fileName = saveDia.getFile();
- if(dirPath==null || fileName==null)
- return;
- ta.setText("");
- File file = new File(dirPath,fileName);
- try
- {
- BufferedWriter bw = new BufferedWriter(new FileWriter(file));
- bw.write(ta.getText());
- bw.flush();
- bw.close();
- }
- catch (IOException b)
- {
- throw new RuntimeException("写入失败");
- }
- }
- });
- mi3.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- openDia.setVisible(true);
- String dirPath = openDia.getDirectory();
- String fileName = openDia.getFile();
- //System.out.println(dirPath+"---"+fileName);
- if(dirPath==null || fileName==null)
- return;
- ta.setText("");
- File file = new File(dirPath,fileName);
- try
- {
- BufferedReader br = new BufferedReader(new FileReader(file));
- String line = null;
- while((line=br.readLine())!=null)
- {
- ta.append(line+"\r\n");
- }
- br.close();
- }
- catch (IOException b)
- {
- throw new RuntimeException("读取失败");
- }
-
- }
- });
- mi.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- public void windowOpened(WindowEvent e)
- {
- System.out.println("启动程序");
- }
- });
- }
- public static void main(String[] args)
- {
- new AwtDemo3();
- }
- }
复制代码 问题见标题,问题在47行到70行之间,哪里有问题?
|