【问题标题】:JAVA Lost focus fom JMenuJAVA JMenu 失去焦点
【发布时间】:2013-11-14 17:40:53
【问题描述】:

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

【问题讨论】:

  • 使用来自 ChongeListener 的 JMenu.getModel(),测试是否 isArmed、isSelected 或 isPressed,但我认为必须有另一个通知程序,否则发布一个 SSCCE,简短、可运行、可编译的原因 a。毫米。将 Jmenu 聚焦在 2 号的问题。鼠标事件
  • 从 MenuListener 或 MenuSelectionManager 和 Swing Action 开始
  • 您误用了 JMenu:它们的意思是作为 menuItems 的容器,而不是作为独立的 menuItems(虽然层次结构有点模糊)。不要,你会混淆你的用户。相反,请考虑使用 JToolBar。

标签: java swing focus jmenu


【解决方案1】:

包装温度;

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 时,这不起作用。否则,它完成了我想要的。我还需要清楚地知道一些事情吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    相关资源
    最近更新 更多