Menu Shortcuts
菜單快捷鍵
The ability to specify menu shortcuts is one of the features in JDK 1.1 -- where menu choices can be selected via the keyboard instead of with the mouse.
在jdk1.1中提供了菜單快捷鍵的實現.快捷鍵---就是利用鍵盤取代鼠標選擇菜單項.
For example:
一個例子:
//導入需要的包
import java.awt.*;
import java.awt.event.*;
//Menu類,實現ActionListener接口
public class menu implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String lab = ((MenuItem)e.getSource()).getLabel();
System.out.println("label = " + lab);
if(lab.equals("Exit"))
{
System.exit(0);
}
}
public static void main(String args[])
{
Frame f = new Frame("testing");
Menu m = new Menu("File");
menu acl = new menu();
MenuItem mi1 = new MenuItem("Open");
mi1.addActionListener(acl);
m.add(mi1);
MenuItem mi2 = new MenuItem("Save");
mi2.addActionListener(acl);
m.add(mi2);
//創建快捷方式,這裏將會依賴於平臺,使用快捷鍵,在Windows下,將會是C+E
MenuShortcut ms3 = new MenuShortcut(KeyEvent.VK_E);
MenuItem mi3 = new MenuItem("Exit", ms3);
mi3.addActionListener(acl);
m.add(mi3);
MenuBar mb = new MenuBar();
mb.add(m);
f.setMenuBar(mb);
f.setSize(200, 200);
f.setVisible(true);
}
}
The example sets up a frame, which is a top-level application window. The frame contains a menu bar, and the menu bar contains a single pull-down menu named "File." The menu offers choices for Open, Save, and Exit, with Exit having a keyboard shortcut set up for it that specifies the virtual key code "E."
這個例子構建了一個Frame作爲頂層窗口,窗口有一個菜單欄,菜單欄裏只有File一項菜單,File菜單提供了Open,Save,Exit三項,Exit項綁定了一個快捷鍵E.
How the shortcut gets invoked varies, depending on the platform in question. For example, with Windows you use Ctrl-E so that when a user types Ctrl-E within the window containing the menu bar, the Exit command is invoked.
快捷鍵通過什麼方式執行是依賴於平臺的,比如,在Windows下,對應的是Ctrl-E,當你按下Ctrl-E時,Exit命令就會執行.
One last thing: this example doesn't actually(事實上,居然) have a command structure(構建,構造) set up for it, but instead invokes actionPerformed to demonstrate(證明) that the command processing structure is in place.
最後一件事:這個例子實際上並沒有構建一個命令處理結構,但是隻是調用actionPerformed證明命令處理結構已經正確執行了。