如果您尝试为不同的JMenuItems 实现侦听器,我会做的是创建一个自定义Action 类,您可以将其用于多个JMenuItems,因为JMenuItems 是何时使用Action 的好例子。
private class MyAction extends AbstractAction {
String name;
public MyAction(String name, Icon icon) {
super(name, icon);
this.name = name;
}
public MyAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelorator) {
super(name, icon);
putValue(Action.SHORT_DESCRIPTION, desc);
putValue(Action.MNEMONIC_KEY, mnemonic);
putValue(Action.ACCELERATOR_KEY, accelorator);
this.name = name;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (name) {
case "menu1Action":
// do something for menuItem1
break;
case "menu2Action":
// do something for menuItem2
break;
case "menu3Action":
// do something for menuItem3
break;
}
}
}
将此类作为HangmanView 的内部类。然后,您可以为每个 JMenuItem 创建此自定义 Action 类的实例。这是一个例子
Action menu1Action = new MyAction(
/* arg 1 */ "menu1Action",
/* arg 2 */ someIcon,
/* arg 3 */ "Some Short description of the action",
/* arg 4 */ new Integer(KeyEvent.VK_T),
/* arg 5 */ KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
- 第一个参数是操作的名称。此名称将是您将在菜单中看到的名称
- 第二个参数是您将在名称旁边的菜单中看到的图标。
- 第三个参数是菜单项动作的描述
- 第四个参数是助记符(即 Alt + T)。
- 第五个参数是加速器(即Ctrl + T)。
当您将Action 添加到JMenu 时,该Action 的标题将自动放置为您在JMenu 中看到的内容。因此,您需要做的就是将此自定义Action 添加到您的JMenu。您根本不必实际创建JMenuItem。 Action 将替代JMenuItem。只需将所有 MyAction 对象添加到 JMenu。
menu.add(menu1Action);
我遗漏的是actionPerformed 中每个单独的switch case 的实现。 case 将是您在构造函数中命名的 action。因为我将Action命名为“menu1Action”,所以我应该在switch case中有对应的名称。在这种情况下,您可以执行您的 JOptionPane 或您希望在单击或通过键盘访问 JMenuItem 时执行的任何其他操作。
使用Action 的另一个好处是它可以用于多种用途。使用您创建的相同MyAction menu1Action,您可以将相同的Action 用于JToolBar。无需对上述menu1Action 进行任何更改,您就可以这样做:
JTooBar toolbar = new JToolBar();
toolbar.add(menu1Action);
现在在您的工具栏和菜单项中,您可以执行相同的操作。工具栏将仅显示图标而不显示名称。
这是一个例子。我所做的是创建三个不同的MyAction 对象。一种用于左对齐,一种用于中心对齐,一种用于右对齐。这些操作中的每一个都分别用于三个单独的组件、一个菜单项、一个收费栏和一个按钮
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionInterfaceDemo extends JFrame {
static JPanel buttonPanel = new JPanel();
static FlowLayout flowLayout = new FlowLayout();
public ActionInterfaceDemo(){
ImageIcon centerIcon = new ImageIcon(
ActionInterfaceDemo.class.getResource("image/centeralignment.png"));
ImageIcon rightIcon = new ImageIcon(
ActionInterfaceDemo.class.getResource("image/rightalignment.png"));
ImageIcon leftIcon = new ImageIcon(
ActionInterfaceDemo.class.getResource("image/leftalignment.png"));
Action leftAction = new MyAction("Left", leftIcon,
"Left alignment for the buttons in the panel",
new Integer(KeyEvent.VK_L),
KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
Action rightAction = new MyAction("Right", rightIcon,
"Right alignment for the buttons in the panel",
new Integer(KeyEvent.VK_R),
KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
Action centerAction = new MyAction("Center", centerIcon,
"Center alignment for the buttons in the panel",
new Integer(KeyEvent.VK_C),
KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
JMenuBar menuBar = new JMenuBar();
JMenu menuAlignment = new JMenu("Alignment");
setJMenuBar(menuBar);
menuBar.add(menuAlignment);
menuAlignment.add(leftAction);
menuAlignment.add(centerAction);
menuAlignment.add(rightAction);
JToolBar toolBar = new JToolBar("Alignment");
toolBar.setBorder(BorderFactory.createLineBorder(Color.BLUE));
toolBar.add(leftAction);
toolBar.add(centerAction);
toolBar.add(rightAction);
buttonPanel.setLayout(flowLayout);
JButton jbtLeft = new JButton(leftAction);
JButton jbtCenter = new JButton(centerAction);
JButton jbtRight = new JButton(rightAction);
buttonPanel.add(jbtLeft);
buttonPanel.add(jbtCenter);
buttonPanel.add(jbtRight);
add(toolBar, BorderLayout.EAST);
add(buttonPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ActionInterfaceDemo();
}
});
}
private class MyAction extends AbstractAction {
String name;
public MyAction(String name, Icon icon, String desc,
Integer mnemonic, KeyStroke accelorator) {
super(name, icon);
putValue(Action.SHORT_DESCRIPTION, desc);
putValue(Action.MNEMONIC_KEY, mnemonic);
putValue(Action.ACCELERATOR_KEY, accelorator);
this.name = name;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (name) {
case "Left":
flowLayout.setAlignment(FlowLayout.LEFT);
break;
case "Right":
flowLayout.setAlignment(FlowLayout.RIGHT);
break;
case "Center":
flowLayout.setAlignment(FlowLayout.CENTER);
break;
}
buttonPanel.revalidate();
}
}
}
您可以在菜单、工具栏或按钮中按“左”,它们将产生相同的结果,因为它们源自相同的Action。
如果你想测试一下,这是我使用的图片
注意您不必使用这些 exact 构造函数中的任何一个。您可以使用不同的参数创建自己的。这只是我喜欢使用的自定义。
也见 How to use Action tutorial