添加菜单:
例:
import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m, subMenu;//里面还可以添加子条目
private MenuItem closeItem, subItem;
MyMenuDemo()
{
init();
}
public void init()
{
f = new Frame("我的窗口");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout()); //流式布局
mb = new MenuBar();
m = new Menu("文件");
closeItem = new MenuItem("退出");
subItem = new MenuItem("小菜单");
subMenu = new Menu("大菜单");
subMenu.add(subItem);//在Menu中可以添加MenuItem
m.add(subMenu);//在Menu中添加Menu,可见Menu是可以扩展的
m.add(closeItem);
mb.add(m);
f.setMenuBar(mb);//在窗口中添加MenuBar
MyEvent();//添加事件
f.setVisible(true);
}
public void MyEvent()
{
closeItem.addActionListener(new ActionListener() //在退出菜单添加事件
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter() //添加窗口事件
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}
--------------------------------------------------------------------------------------------------
文件打开保存,使用默认的边界式布局
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest
{
private Frame f;
private TextArea ta;
private MenuBar bar;
private Menu fileMenu;
private MenuItem openItem, saveItem, closeItem, subItem;
private FileDialog openDia, saveDia;
private File file;
MyMenuTest()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
//f.setLayout(new FlowLayout());
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
closeItem = new MenuItem("退出");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
bar.add(fileMenu);
f.setMenuBar(bar);
openDia = new FileDialog(f, "我要打开", FileDialog.LOAD);//有两种模式load和save
saveDia = new FileDialog(f, "我要保存", FileDialog.SAVE);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
saveItem.addActionListener(new ActionListener() //保存
{
public void actionPerformed(ActionEvent e)
{
if(file==null)//如果文件不存在,则弹出保存对话框
{
saveDia.setVisible(true);//显示对话框
String dirPath = saveDia.getDirectory(); //文件路径
String fileName = saveDia.getFile(); //文件名
if(dirPath==null || fileName==null)
return;
file = new File(dirPath, fileName);
}
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();//获取文本中的内容
bufw.write(text);//写到文件中
//bufw.flush();
bufw.close();
}
catch(IOException ex)
{
throw new RuntimeException();
}
}
});
openItem.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 = new File(dirPath, fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine()) != null)//读取文件内容
{
ta.append(line+"\r\n");//添加一行并回车换行
}
bufr.close();
}
catch(IOException ex)
{
throw new RuntimeException("读取失败");
}
}
});
closeItem.addActionListener(new ActionListener()//退出菜单事件
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuTest();
}
}
---------------------------------------------------------------------------------
个人总结:要熟悉菜单MenuBar、Menu、MenuItem之间的逻辑关系,掌握给菜单
添加事件的基本方法,打开文件时要判断文件是否存在,若已经有内容,则要清空。
要掌握FileDialog.Load和FileDialog.SAVE的两种模式