本帖最后由 陌路行者 于 2013-7-13 10:37 编辑
- <p>import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- class MyWinDemo
- {
- private Frame f;
- private Menu fileMenu;
- private MenuBar bar;
- private MenuItem openItem,saveItem,exit;
- private TextArea ta;
- private Dialog openDia,saveDia;
- private File file;
- MyWinDemo()
- {
- init();
-
- }
- public void init()
- {
- f = new Frame("My window");
- f.setBounds(300,240,500,420);
- bar = new MenuBar();
- f.setMenuBar(bar);
- fileMenu = new Menu("文件");
- bar.add(fileMenu);
- openItem = new MenuItem("打开");
- saveItem = new MenuItem("保存");
- exit = new MenuItem("退出");
- openDia = new FileDialog(f,"open",FileDialog.LOAD);
- openDia = new FileDialog(f,"closed",FileDialog.SAVE);
- fileMenu.add(openItem);
- fileMenu.add(saveItem);
- fileMenu.add(exit);
- ta = new TextArea();
- f.add(ta);
- myEvent();
- f.setVisible(true);
- }
- public void myEvent()
- {
- openItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- openDia.setVisible(true);
- String dirPath = openDia.getDirectory();
- String fileName = openDia.getFile();
- if(dirPath==null || fileName == null)
- return;
- file = new File(dirPath,fileName);
- try
- {
- BufferedReader bufr = new BufferedReader(new FileReader(file));
- String line = null;
- while((bufr.readLine())!=null)
- {
- ta.append(line);
- //bufr.flush();
- }
- bufr.close();
- }
- catch (IOException ex)
- {
- throw new RuntimeException("文件读取失败");
- }
- }
- });
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- exit.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- }
- }
- class MyWindow
- {
- public static void main(String[] args)
- {
- new MyWinDemo();
- }
- }
复制代码 找的眼都花了,
|