菜单上的java动作侦听器,而不是菜单项 [英] java action listener on menu, and not on menu item
问题描述
我需要启蒙.
如何添加actionactionListener事件绑定到菜单,而不绑定到菜单ITEM这是演示代码,可以运行(在 menuITEM 上).
how to add action actionListener event bind to the menu, and not bind to the menu ITEM here is the demo code, that works(on menuITEM)..
menuFileItem.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("It works");
}
}
);
但是当我尝试相同时,但仅在菜单本身上它不起作用!
but when i try the same , but just on the MENU itself it doesn't work!
menuFile.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("Plz work... :( ");
}
}
);
是否可以将侦听器添加到菜单中?我教过听众可以添加到一切中.
is it possible to add listener to menu? i taught listener could be added to everything.
推荐答案
您可以将 ActionListener
添加到 JMenu
,因为此方法继承自 AbstractButton代码>.(JMenu 文档)
You can add an ActionListener
to a JMenu
as this method is inherited from AbstractButton
. (JMenu Documentation)
JMenu menu = new JMenu("MyMenu");
menu.addActionListener(new ActionListener(){...});
但是,它不打算这样使用:JMenu 忽略 ActionEvent.您应该改用 MenuEvent
和 MenuListener
.
But, it is not intended to be used this way: JMenu ignores ActionEvent. You should use MenuEvent
and MenuListener
instead.
JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
System.out.println("menuSelected");
}
@Override
public void menuDeselected(MenuEvent e) {
System.out.println("menuDeselected");
}
@Override
public void menuCanceled(MenuEvent e) {
System.out.println("menuCanceled");
}
});
这篇关于菜单上的java动作侦听器,而不是菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!