java 单击 JMenuItem 时执行操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1726115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:44:24  来源:igfitidea点击:

Performing an action when an JMenuItem is clicked?

javaswingactionlistener

提问by TheQuizitor

So i have made a simple program with a basic menu at the top of the frame, Now i just need to put actions behind each JMenuItem. Im struggling to work the code out though, Here is what i thought would work:

所以我制作了一个简单的程序,在框架的顶部有一个基本菜单,现在我只需要在每个 JMenuItem 后面放置操作。不过,我正在努力解决代码,这是我认为可行的方法:

JMenu file_Menu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit Program"); 
file_Menu.add(fileExit);
fileExit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        JFrame hello = new JFrame("POPUP");
        hello.setSize(100,75);
        hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
        hello.setVisible(true);
    }
});
main_Menu.add(file_Menu);

This doesn't seem to work though, I thought that this code would create a small popup window when the menu item is clicked.

但这似乎不起作用,我认为此代码会在单击菜单项时创建一个小弹出窗口。

Can any spot the bug because i cant seem to.

任何人都可以发现错误,因为我似乎无法找到。

回答by David Moles

Suggestion: Instead of adding a separate ActionListener, just use AbstractAction:

建议:不要添加单独的ActionListener,只需使用AbstractAction

JMenuItem fileExit = new JMenuItem(new AbstractAction("Exit Program") {
    public void actionPerformed(ActionEvent ae) {
        JFrame hello = new JFrame("POPUP");
        hello.setSize(100,75);
        hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
        hello.setVisible(true);
    }
});

I'd also suggest, instead of setting EXIT_ON_CLOSEon the popup menu, you set it on the main frame of your application, and have the action simply call theMainFrame.dispose().

我还建议,不要EXIT_ON_CLOSE在弹出菜单上进行设置,而是将其设置在应用程序的主框架上,并且只需调用theMainFrame.dispose().

回答by Noel Ang

You got it working, but you have another problem.

你让它工作了,但你还有另一个问题。

Don't do this:

不要这样做:

hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);

When you close the pop-up frame, your entire JVM terminates. Consult JFrame.setDefaultCloseOperation javadocs for a more appropriate value.

当您关闭弹出框时,整个 JVM 将终止。请参阅 JFrame.setDefaultCloseOperation javadocs 以获得更合适的值。

回答by Dmitry

Give an instance of Action (extend from AbstractAction) to JMenuItem

将 Action 的实例(从 AbstractAction 扩展)提供给 JMenuItem

回答by camickr

Based on the code you posted it looks like it should work, but we can't see the entire context of how the menu item is being used.

根据您发布的代码,它看起来应该可以工作,但我们无法看到菜单项使用方式的整个上下文。

Did you debug your code (with a System.out.println) to see if the ActionListener is being invoked?

您是否调试了代码(使用 System.out.println)以查看是否正在调用 ActionListener?

If you need more help post your SSCCEthat demonstrates the problem.

如果您需要更多帮助,请发布说明问题的SSCCE

回答by TheQuizitor

Fixed it.

解决它。

Forgot to add the actionPerformed method.

忘记添加 actionPerformed 方法。