?? menutest.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MenuTest
{
public static void main(String[] args)
{
//創建框架窗口
final Frame f = new Frame("My First Java Program");
f.setSize(600, 400);
f.setLocation(200, 100);
//創建菜單
MenuBar mb = new MenuBar();
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
MenuItem mi1 = new MenuItem("New");
MenuItem mi2 = new MenuItem("Open");
MenuItem mi3 = new MenuItem("Close");
MenuItem mi4 = new MenuItem("Exit");
MenuItem mi5 = new MenuItem("Copy");
MenuItem mi6 = new MenuItem("Paste");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
//創建文本框
final TextArea ta = new TextArea();
f.add(ta);
//菜單命令響應
mi4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//退出系統
System.exit(0);
}
});
mi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//顯示文件對話框
FileDialog fd = new FileDialog(f, "Loading File", FileDialog.LOAD);
fd.show();
//以下代碼要等文件窗口關閉后才執行
System.out.println("after show fileDialog!");
String strFile = /*fd.getDirectory() + */fd.getFile();
byte[] data = new byte[10 * 1024];
if (strFile != null)
{
try
{
FileInputStream fis = new FileInputStream(strFile);
int len = fis.read(data);
ta.append(new String(data, 0, len));
}
catch(FileNotFoundException ex)
{
System.out.println(ex);
}
catch(IOException ex)
{
System.out.println(ex);
}
}
}
});
//設置框架窗口菜單對象
f.setMenuBar(mb);
//框架窗口事件委托
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.show();
//顯示框架窗口后,下面代碼會馬上緊跟執行
System.out.println("after show frame!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -