- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class MyMenuDemo
- {
- private Frame f;
- private Menu fileMenu,subMenu;
- private MenuBar mbar;
- private Button btn;
- private MenuItem openItem,saveItem,closeItem;
-
- private TextArea ta;
- private FileDialog dia,saveDia;
- private File file;
-
- MyMenuDemo()
- {
- init();
- }
- public void init()
- {
- f =new Frame("MyMenuDemo");
- f.setBounds(300,200,500,400);
- // f.setLayout(new FlowLayout());流式布局,默认的话是边界布局
-
- mbar =new MenuBar();
- fileMenu =new Menu("文件");
- //subMenu =new Menu("子菜单");//声明子类new父类是不可以的:subMenu=new MenuItem();
- saveItem =new MenuItem("保存");//声明父类new子类是可以的:subItem =new Menu();
- //subMenu.add(subItem);
-
- openItem =new MenuItem("打开");
- closeItem =new MenuItem("关闭");
- fileMenu.add(openItem);
- fileMenu.add(saveItem);
- fileMenu.add(closeItem);
- mbar.add(fileMenu);
- f.setMenuBar(mbar);
- ta =new TextArea();
- f.add(ta);
-
- dia =new FileDialog(f,"打开文件-test",FileDialog.LOAD);
- saveDia =new FileDialog(f,"保存文件-test",FileDialog.SAVE);
- myEvent();
- f.setVisible(true);
- }
- public void myEvent()
- {
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
-
- closeItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- openItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- dia.setVisible(true);
- String fileDir =dia.getDirectory();
- String fileName =dia.getFile();
- //System.out.println(fileDir+""+fileName);
- if (fileDir==null || fileName==null)
- {
- return ;
- }
-
-
- ta.setText("");
- file =new File(fileDir,fileName);
- sop((file==null)==(fileDir==null || fileName==null));
- BufferedReader bufr =null;
- try
- {
- bufr =new BufferedReader(new FileReader(file));
- String line=null;
- while((line=bufr.readLine())!=null)
- {
- ta.append(line+"\r\n");
- }
- }
- catch (IOException ex)
- {
- throw new RuntimeException("读取失败");
- }
- finally
- {
- try
- {
- if (bufr!=null)
- {
- bufr.close();
- }
- }
- catch (IOException ee)
- {
- throw new RuntimeException("关闭文件失败");
- }
- }
- }
- });
- saveItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- if (file==null)
- {
- saveDia.setVisible(true);
-
-
- String filePath=saveDia.getDirectory();
- String fileName=saveDia.getFile();
- if (filePath ==null || fileName==null)
- return ;
- file =new File(filePath,fileName);
- } //一个 括号引发的学案
- try
- {
- BufferedWriter bufw =new BufferedWriter(new FileWriter(file));
- String text=ta.getText();
- //sop(text+"111");
- bufw.write(text);
- bufw.close();
- }
- catch (IOException ee)
- {
- throw new RuntimeException("保存失败");
- }
- }
- });
- }
- public static void main(String[] args)
- {
- new MyMenuDemo();
- File f =new File("D:\\","copy.txt");
- String filePath=f.getParent();
- String fileName=f.getName();
- sop(f);
- sop((f==null)==(filePath==null || fileName==null));
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
复制代码 |
|