java.lang.Object
--java.awt.Component
--java.awt.Container
--javax.swing.JComponent
--javax.swing.AbstractButton
--javax.swing.JMenuItem
JMenuItem继承AbstractButton类,因此JMenuItem具有许多AbstractButton的特性,也可以说JMenuItem是一种特殊的Button,所 以JMenuItem支持许多在Button中好用的功能,例如加入图标文件或是当我们在菜单中选择某一项JMenuItem时就如同按下按钮的操作 一样触发ActionEvent,通过ActionEvent的机制我们就能针对不同的JMenuItem编写其对应的程序区段,我们来看看JMenuItem的构造 方式有哪些:
在看过JMenuItem的构造方式之后,我们来看一个例子了解JMenuItem是如何运作的.这个例子延伸自JMenu1.java,改写 buileFileMenu()方法部份.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class JMenuItem1 extends JFrame {
JTextArea theArea = null;
public JMenuItem1() {
super("JMenu1");
theArea = new JTextArea();
theArea.setEditable(true);
getContentPane().add(new JScrollPane(theArea));
JMenuBar MBar = new JMenuBar();
JMenu mfile = buildFileMenu();
MBar.add(mfile);
setJMenuBar(MBar);
}// end of JMenu1()
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("文件");
JMenuItem newf = new JMenuItem("新建");
JMenuItem open = new JMenuItem("打开");
JMenuItem close = new JMenuItem("关闭");
JMenuItem exit = new JMenuItem("退出");
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();// 分隔线
thefile.add(exit);
return thefile;
}// end of buildFileMenu()
public static void main(String[] args) {
SwingUtil.setLookAndFeel();
JFrame F = new JMenuItem1();
F.setSize(400, 200);
F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});// end of addWindowListener
F.setVisible(true);
} // end of main
}// end of class JMenu1
class SwingUtil {
public static final void setLookAndFeel() {
try {
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration",
"true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
}
}
我们在一开始就提到JMenuItem是一个特殊的Button组件,因此我们可以在JMenuItem中加入图标文件来美化界面的显示状态,我 们来看下面这个范例:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import com.incors.plaf.alloy.*;
import com.incors.plaf.alloy.themes.glass.*;
public class JMenuItem2 extends JFrame {
JTextArea theArea = null;
public JMenuItem2() {
super("JMenuItem2");
theArea = new JTextArea();
theArea.setEditable(false);
getContentPane().add(new JScrollPane(theArea));
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu();
MBar.add(mfile);
setJMenuBar(MBar);
}// end of JMenuItem2()
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
JMenuItem newf = new JMenuItem("New", new ImageIcon("icons/new24.gif"));
JMenuItem open = new JMenuItem("Open",
new ImageIcon("icons/open24.gif"));
JMenuItem close = new JMenuItem("Close", new ImageIcon(
"icons/close24.gif"));
JMenuItem quit = new JMenuItem("Exit",
new ImageIcon("icons/exit24.gif"));
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}// end of buildFileMenu()
public static void main(String[] args) {
SwingUtil.setLookAndFeel();
JFrame F = new JMenuItem2();
F.setSize(400, 200);
F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});// end of addWindowListener
F.setVisible(true);
} // end of main
}// end of class JMenuItem2
class SwingUtil {
public static final void setLookAndFeel() {
try {
Font font = new Font("JFrame", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration",
"true");
AlloyTheme theme = new GlassTheme();
LookAndFeel alloyLnF = new AlloyLookAndFeel(theme);
UIManager.setLookAndFeel(alloyLnF);
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
}
}
运行程序我们可看到在文字左边出现了我们所指定的图标文件.这时我们又会有疑问了,图标文件一定要摆在文字的左边吗?答案 是不一定的!图标在这里会出现在文字左边是因为JMenuItem默认值的关系,我们可以利用setHorizontalTextPosition()方法改变文 字的位置.我们将上面程序的buildFileMenu()方法改写如下:
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
JMenuItem newf = new JMenuItem("New",new ImageIcon("icons/new24.gif"));
JMenuItem open = new JMenuItem("Open",new ImageIcon("icons/open24.gif"));
JMenuItem close= new JMenuItem("Close",new ImageIcon("icons/close24.gif"));
JMenuItem quit = new JMenuItem("Exit",new ImageIcon("icons/exit24.gif"));
//定义文字位置
//在这里我们使用了SwingConstants这个interface,这个interface定义了许多在Swing中常用的数值,由于AbstractButton
//类实作了SwingConstants interface,所以我们可以直接使用SwingConstants.LEFT代表JMenuItem组件的最左边界值.
newf.setHorizontalTextPosition(SwingConstants.LEFT);
open.setHorizontalTextPosition(SwingConstants.LEFT);
close.setHorizontalTextPosition(SwingConstants.LEFT);
quit.setHorizontalTextPosition(SwingConstants.LEFT);
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}//end of buildFileMenu()
到目前为止我们已经能将一个菜单的外观构造起来,不过我们还少了一个小小的功能,那就是快捷键的设置.快捷键是让我们能使 用键盘来控制菜单并方便操作之用.那么怎么将它加入到菜单中呢?我们来看看下面的范例是怎么实现的. 在JMenuItem2.jav中修改buildFileMenu()方法:操作方式->:[Alt]+快捷键
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf = new JMenuItem("New",new ImageIcon("icons/new24.gif"));
JMenuItem open = new JMenuItem("Open",new ImageIcon("icons/open24.gif"));
JMenuItem close= new JMenuItem("Close",new ImageIcon("icons/close24.gif"));
JMenuItem quit = new JMenuItem("Exit",new ImageIcon("icons/exit24.gif"));
newf.setMnemonic('N');
open.setMnemonic('O');
close.setMnemonic('C');
quit.setMnemonic('X');
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}//end of buildFileMenu()
上面的方法是以[Alt+快捷键]操作,一般的快捷键会设置为[Ctrl]加上某个字符,来直接运行某项功能,要在菜单中加入快捷键很 简单,只需要使用setAccelerator()方法即可,我们来看下面的范例:
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf = new JMenuItem("New",new ImageIcon("icons/new24.gif"));
JMenuItem open = new JMenuItem("Open",new ImageIcon("icons/open24.gif"));
JMenuItem close= new JMenuItem("Close",new ImageIcon("icons/close24.gif"));
JMenuItem quit = new JMenuItem("Exit",new ImageIcon("icons/exit24.gif"));
newf.setMnemonic('N');
open.setMnemonic('O');
close.setMnemonic('L');
quit.setMnemonic('X');
newf.setAccelerator( KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false) );
open.setAccelerator( KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false) );
close.setAccelerator( KeyStroke.getKeyStroke('L', java.awt.Event.CTRL_MASK, false) );
quit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false) );
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}//end of buildFileMenu()
说明:利用setAccelerator()方法,分别设置各个JMenuItem的快捷键值.在setAccelerator()方法中使用了KeyStroke类,这个类是 用来管理键盘上的各种信息,我们利用getKeyStroke()方法来指定快捷键的键值.getKeyStroke()方法的三个字段值分别代表键值, 屏蔽键值各放开按键时是否触发事件.在这个范例里我们设置[Ctrl+英文字符]来运行JMenuItem的选项功能. 当然,快捷键的屏蔽键值不一定要是[Ctrl],也可以是[Alt],[Shift].
newf.setAccelerator( KeyStroke.getKeyStroke('N', java.awt.Event.SHIFT_MASK, false) );
open.setAccelerator( KeyStroke.getKeyStroke('O', java.awt.Event.SHIFT_MASK, false) );
close.setAccelerator( KeyStroke.getKeyStroke('L', java.awt.Event.SHIFT_MASK, false) );
quit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.SHIFT_MASK, false) );
现在我们已经能够完整的建立一个菜单了,但是我们现在看到的菜单都是单一层次的,如何来建立具有层次式的菜单呢?我们来看 下面的范例:
由于这支程序修改自JMenuItem3.java,由于只修改了bulidMenu()方法,所以在这里我们只列出bulidMenu()方法的程序部份:
public JMenu buildFileMenu() {
JMenu thefile = new JMenu("File");
thefile.setMnemonic('F');
JMenuItem newf = new JMenuItem("New",new ImageIcon("icons/new24.gif"));
JMenuItem open = new JMenuItem("Open",new ImageIcon("icons/open24.gif"));
JMenuItem close= new JMenuItem("Close",new ImageIcon("icons/close24.gif"));
JMenuItem quit = new JMenuItem("Exit",new ImageIcon("icons/exit24.gif"));
newf.setMnemonic('N');
open.setMnemonic('O');
close.setMnemonic('L');
quit.setMnemonic('X');
newf.setAccelerator( KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false) );
open.setAccelerator( KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false) );
close.setAccelerator( KeyStroke.getKeyStroke('L', java.awt.Event.CTRL_MASK, false) );
quit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false) );
JMenu prefMenu = new JMenu("Preferences..");
prefMenu.setMnemonic('P');
JMenuItem setPage = new JMenuItem("setPage",new ImageIcon("icons/setpage24.gif"));
JMenuItem setImport = new JMenuItem("Import",new ImageIcon("icons/import24.gif"));
JMenuItem setExport = new JMenuItem("Export",new ImageIcon("icons/export24.gif"));
setPage.setMnemonic('S');
setImport.setMnemonic('I');
setExport.setMnemonic('E');
setPage.setAccelerator( KeyStroke.getKeyStroke('S', java.awt.Event.CTRL_MASK, false) );
setImport.setAccelerator( KeyStroke.getKeyStroke('I', java.awt.Event.CTRL_MASK, false) );
setExport.setAccelerator( KeyStroke.getKeyStroke('E', java.awt.Event.CTRL_MASK, false) );
prefMenu.add(setPage);
prefMenu.add(setImport);
prefMenu.add(setExport);
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();
thefile.add(prefMenu);
thefile.addSeparator();
thefile.add(quit);
return thefile;
}//end of buildFileMenu()