JMenu:来自Jmenu的触发事件用于打开dialoge(确认)Joption是/否选项点击两次工作

时间:2016-02-25 11:02:39

标签: java swing jmenuitem

我在JMenu(退出)上编写代码时单击它打开JOption窗格确认消息框,带有是/否选项但是第一次弹出时没有得到焦点是/否按钮应该被点击两次才能工作。

挖掘后我意识到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);
               }
            }
        });

1 个答案:

答案 0 :(得分:2)

JMenu并非设计用于此目的,您应该使用JMenuItem

首先查看How to Use MenusHow 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]按钮关闭窗口,它将执行相同的操作,检查用户是否要退出