JAVA失去焦点fom JMenu

时间:2013-11-14 17:40:54

标签: java swing focus jmenu

这是我的情况,我有JMenubar并且有CardLayout。事实上,我使用JMenu移动每张卡,所以JMenus没有任何JMenuItem(这有点奇怪,是吧?)。例如,我设置按钮显示消息。麻烦的是当我点击JMenu,然后点击按钮。在我点击按钮2次之前,它不会立即显示消息。我认为这是焦点,并尝试使用button.requestFocusInWindow()。但它失败了。那么,我需要有人帮助我吗?

1 个答案:

答案 0 :(得分:0)

包Temp;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class PopupMenuDemo2 extends JFrame{
    public static Container createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());

        JButton button = new JButton("click");
        button.setSize(80, 80);
        button.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                System.out.println("button's clicked");
            }
        });
        contentPane.setLayout(new CardLayout());
        contentPane.add(button);

        return contentPane;
    }

    public static JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu1;
        JMenu menu2;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu1 = new JMenu("A Menu");
        menuBar.add(menu1);

        //Build second menu in the menu bar.
        menu2 = new JMenu("Another Menu");
        menu2.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) {
                System.out.println("mouse press on menu2");
            }
        });
        menuBar.add(menu2);
        return menuBar;
    }

    private static void createAndShowGUI() {
        PopupMenuDemo2 pmn2 = new PopupMenuDemo2();
        pmn2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create/set menu bar and content pane.
        pmn2.setJMenuBar(createMenuBar());
        pmn2.setContentPane(createContentPane());

        //Display the window.
        pmn2.setSize(450, 260);
        pmn2.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(PopupMenuDemo2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(PopupMenuDemo2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(PopupMenuDemo2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(PopupMenuDemo2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
        //</editor-fold>

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我找到原因。但我真的不明白。当我取消注释final try-catch statement Set Look And Feel时,这不起作用。否则,它完成了我想要的。我还需要清楚地了解一些事情吗?