標籤:io os java ar for 檔案 sp cti on
import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class NotePad {
private Frame f = new Frame("記事本");
private MenuBar mb = new MenuBar();
Menu file = new Menu("檔案");
Menu edit = new Menu("編輯");
MenuItem newItem = new MenuItem("建立");
MenuItem saveItem = new MenuItem("儲存");
MenuItem exitItem = new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
CheckboxMenuItem autoWrap = new CheckboxMenuItem("自動換行");
MenuItem copyItem = new MenuItem("複製");
MenuItem pasteItem = new MenuItem("粘貼");
Menu format = new Menu("格式");
MenuItem commentItem = new MenuItem("注釋",new MenuShortcut(KeyEvent.VK_SLASH , true));
MenuItem cancelItem = new MenuItem("取消注釋");
private TextArea ta = new TextArea(6 , 40);
public void init()
{
//匿名內部類建立菜單監聽器
ActionListener menuListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
ta.append("單擊“"+cmd+"”菜單"+"\n");
if (cmd.equals("退出"))
{
System.exit(0);
}
}
};
//為commentItem菜單添加事件監聽器
commentItem.addActionListener(menuListener);
exitItem.addActionListener(menuListener);
file.add(newItem);
file.add(saveItem);
file.add(exitItem);
edit.add(autoWrap);
edit.addSeparator();
edit.add(copyItem);
edit.add(pasteItem);
format.add(commentItem);
format.add(cancelItem);
edit.add(new MenuItem("-"));
edit.add(format);
mb.add(file);
mb.add(edit);
f.setMenuBar(mb);
//匿名內部類建立監聽器對象
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.add(ta);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new NotePad().init();
}
}
一個記事本Demo