English 中文(简体)
如何将 JMenuTemit 和 JButton 连接起来
原标题:How to link a JMenuItem to a JButton

Let s say I have a JMenuItem with a text inside "Exit", and a JButton with the text "Exit", the command which JButton will use is System.exit(0), of course using Action Listener, Ok i Know, I can put the same codes when clicking on the JMenuItem, but isn t there a way, that when I click on the JMenuItem, the JButton is clicked so then the following commands are executed (JButton commands)?

最佳回答

您能够做的是创建 Action 对象, 并且对您的 JButton 和您的 JMEMenuITEM 使用该对象。

Action exit = new AbstractAction() {
        private static final long serialVersionUID = -2581717261367873054L;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
exit.putValue(Action.NAME, "Exit");
exit.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X);

JButton exitButton = new JButton(exit);
JMenuItem exitItem = new JMenuItem(exit);
问题回答

这样做的好办法就是将相同的 ActionListener 设置为两个组成部分。 例如 :

JButton button = new JButton ("Exit");
JMenuItem item = new JMenuItem ("Exit");

ActionListener exitaction = new ActionListener ()
{
    public void actionPerformed (ActionEvent e)
    {
        System.exit (0);
    }
};

button.addActionListener (exitaction);
item.addActionListener (exitaction);

然而,我建议不要使用 System.exit (0) 。关闭程序的最好方式(我假定关闭程序的最好方式基本上是设置 JFrame )

frame.set Defaulclos operation (JFrame.DISPOSE_ON_CLOSE) /code 操作(JFrame.DISPOSE_ON_CLOSE)

(其中 frame 是程序的窗口)

ActionListener 中调用 frame.dispect () () ActionListener

您可以尝试将按钮保存为类字段

private JButton button;

并在单击菜单项的事件处理器中插入代码

button.doClick();

的解决方案“https://stackoverflow.com/a/10739951/611595>>Sobolan 更优雅。

我认为最好的方法就是 在JMenuItem和JButton的听众中 登记同样的“倾听行动者”案例, 就像使用旧的“指挥”设计模式。

我建议不要试图欺骗事件引擎, 比如让JMenuIot 点燃与JButton的紧迫性相关的事件, 因为这不是正在发生的事情, 而是你似乎想要的, 就是把这两种行动 重新用于两个不同的事件。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签