12、菜单-文件打开与保存
package com.edu.Tewnty_Two;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MyMenuDemo {
private Frame f;
private MenuBar mb;
//文本
private TextArea ta;
//
private File file;
private Menu m,m2,subMenu,fileMenu;
private MenuItem closeItem,subItem,subItem2,clearItem;
private MenuItem openItem,saveItem;
MyMenuDemo()
{
init();
}
public void init()
{
f=new Frame("my frame");
//对frame进行基本设置
f.setBounds(30,500,600,600);
//f.setLayout(new FlowLayout());
mb=new MenuBar();
ta=new TextArea();
m=new Menu("文件");
m2=new Menu("清空");
subMenu=new Menu("子菜单");
fileMenu=new Menu("文件操作");
subItem=new MenuItem("子条目");
subItem2=new MenuItem("子条目2");
closeItem=new MenuItem("退出");
clearItem=new MenuItem("OK");
saveItem=new MenuItem("保存文件");
openItem=new MenuItem("打开文件");
subMenu.add(subItem);
subMenu.add(subItem2);
fileMenu.add(openItem);
fileMenu.add(saveItem);
m.add(fileMenu);
m.add(subMenu);
m.add(closeItem);
m2.add(clearItem);
mb.add(m);
mb.add(m2);
f.setMenuBar(mb);
openDialog=new FileDialog(f,"open file",FileDialog.LOAD);
saveDialog=new FileDialog(f,"save file",FileDialog.SAVE);
myEvent();
f.add(ta);
f.setVisible(true);
}
private FileDialog openDialog,saveDialog;
private void myEvent()
{
//清空文本框
clearItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ta.setText("");
}
});
//菜单保存
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(file==null)
{
saveDialog.setVisible(true);
String dirpath=saveDialog.getDirectory();
String filename=saveDialog.getFile();
if(filename==null||dirpath==null)
return ;
file=new File(dirpath,filename);
}
try {
BufferedWriter bufw=
new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.close();
} catch (Exception e2) {
// TODO: handle exception
System.out.println("保存出现错误");
}
}
});
//菜单打开
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
openDialog.setVisible(true);
String dirpath=openDialog.getDirectory();
String filename=openDialog.getFile();
System.out.println(filename+":"+dirpath);
if(filename==null||dirpath==null)
return ;
ta.setText("");
file=new File(dirpath,filename);
try {
BufferedReader bufr=
new BufferedReader(new FileReader(file));
String lineString=null;
if((lineString=bufr.readLine())!=null)
{
ta.append(lineString+"\r\n");
}
bufr.close();
} catch (IOException e2) {
// TODO: handle exception
System.out.println("出现错误");
}
}
});
//菜单退出
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Menu closing");
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
System.out.println("window closing");
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyMenuDemo();
}
}
|
|