【问题标题】:JMenu consumes focuslost event in Windows7 LAF Java7JMenu 在 Windows7 LAF Java7 中使用 focuslost 事件
【发布时间】:2012-09-03 16:08:37
【问题描述】:

如果单击另一个组件时弹出菜单仍然打开,则该组件不会收到事件,因为它可能已被弹出窗口消耗。一般情况下,所有 JPopupmenus 都会发生这种情况。 这只发生在带有 Windows LAF (Windows7) 的 Java 7 中。有解决方法吗?这是一个已知的错误吗?

import javax.swing.*;
import java.awt.event.*;

public class Test
{
    public static void main(String[] s)
    throws Exception
    {
           String lookAnfFeelClassName = UIManager.getSystemLookAndFeelClassName();
           UIManager.setLookAndFeel(lookAnfFeelClassName);

           JMenu menu = new JMenu("TEST Menu");
           JMenuItem menuItem = new JMenuItem("Menu Item 1");

           JMenuBar menuBar = new JMenuBar();
           menu.add(menuItem);
           menuBar.add(menu);

           final JButton b = new JButton("Test");
           b.setBounds(5, 50, 60, 20);
           b.addActionListener(new ActionListener()
           {
                  public void actionPerformed(ActionEvent e)
                  {
                        //If the Menu is open when I press the button, the putton is not pressed 
                        //so I have to press it again. 
                        JOptionPane.showMessageDialog(b, "Button Pressed");
                  }
           }
           );

           JFrame frame = new JFrame();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(150, 150);
           frame.setJMenuBar(menuBar);
           frame.getContentPane().setLayout(null);
           frame.getContentPane().add(b);
           frame.setVisible(true);           
    }
}

【问题讨论】:

  • 请问JRE和JDK版本编译了什么???
  • JRE 版本 1.7.0_05-b05,但我可以用 JRE 1.6.0_23 重现它
  • 在 Win7 上用 SystemLookAndFeel 重现???,嗯,等几分钟我要重新启动到 Win7
  • 是的,在 windows7 中你会立即复制它
  • 就在Win7 上,您必须两次单击JButton,在禁用SystemLookAndFeel 后不会出现此问题,对于jdk1.6.0_25 和jdk1.7.0_04,我必须测试在WinXPJava6

标签: java swing java-7 jmenu jpopupmenu


【解决方案1】:

这是解决问题的妙招:

UIManager.put("PopupMenu.consumeEventOnClose", Boolean.FALSE);

我在查看 BasicPopupMenuUI 类的源代码后发现了这一点。显然,根据代码中的以下 cmets,这种行为是经过深思熟虑的设计选择,但对我来说确实像是一个错误。

            // Ask UIManager about should we consume event that closes
            // popup. This made to match native apps behaviour.

顺便说一句,Java 5 和 6 也有这种情况。

【讨论】:

  • 感谢修复。
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2012-01-03
  • 1970-01-01
  • 2018-06-07
相关资源
最近更新 更多