JMenu : 从 Jmenu 触发事件打开对话框(确认框) Joption yes/No 选项点击两次工作

JMenu : while trigger event from Jmenu for open dialoge (confirmbox) Joption yes/No option click twice to work

我在 JMenu(Exit) 上编写代码,单击它会打开带有 yes/NO 选项的 JOption Pane 确认消息框,但是当它弹出 yes/no 按钮时,它第一次没有获得焦点应该被点击两次上班。

在挖掘之后,我意识到 Jmenu(退出)选项在单击任何按钮后生成弹出窗口时不会失去焦点,它会在下次触发功能时获得焦点,所以我该如何处理这种情况。

JMenu menu5 = new JMenu("Exit");
        menu5.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                requestFocus();
                callpopUp();
            }

            private void callpopUp() {
                int  choice=JOptionPane.YES_OPTION;
                choice = JOptionPane.showConfirmDialog(null, "Are you sure to Exit Application",
                       "Confirmation", JOptionPane.YES_NO_OPTION);


               if (choice == JOptionPane.YES_OPTION) {
                System.out.println("Exit Button Clicked.");
                   System.exit(0);
               }
            }
        });

JMenu 不是为此目的而设计的,您应该使用 JMenuItem

首先查看 How to Use Menus and How to Write an Action Listeners 了解更多详情

类似...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    private JFrame frame;

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JMenuBar mb = new JMenuBar();
                JMenu file = new JMenu("File");
                JMenuItem exit = new JMenuItem("Exit");
                exit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        performClose();
                    }
                });

                file.add(exit);
                mb.add(file);

                frame = new JFrame("Testing");
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        performClose();
                    }

                });
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setJMenuBar(mb);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected void performClose() {
        int choice = JOptionPane.YES_OPTION;
        choice = JOptionPane.showConfirmDialog(null, "Are you sure to Exit Application",
                        "Confirmation", JOptionPane.YES_NO_OPTION);

        if (choice == JOptionPane.YES_OPTION) {
            System.out.println("Exit Button Clicked.");
            frame.setVisible(false);
            frame.dispose();
        }
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

这将允许您使用 File->Exit 菜单选项或简单地通过 [X] 按钮关闭 window,它会执行相同的操作,检查用户是否想要退出