0

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

4

1 回答 1

0

包装温度;

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。否则,它就如我所愿。我还需要清楚地知道一些事情吗?

于 2013-11-15T06:25:46.823 回答