3

我需要用我的自定义组件显示一个摆动弹出窗口。弹出窗口应该保持可见,直到我自己隐藏它,但不应该得到焦点。

我有一个由其他开发人员编写的代码,它通过以下方式完成:

       popupMenu = new JPopupMenu();
       popupMenu.add(myCustomComponent, BorderLayout.CENTER);
       popupMenu.setFocusable(false);
       popupMenu.setVisible(true);
       popupMenu.show(parentComponent, x, y);

这似乎可行,但有一个错误 - 当弹出窗口可见时,弹出窗口会消耗组件外部的第一次鼠标单击。所以我需要单击两次才能将焦点设置到另一个组件。

我该如何解决?或者什么是制作弹出窗口的正确方法?

更新

最后,我设法在短代码片段中重现了我的问题。感谢 Guillaume Polet 给了我一个起点。

这是代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class TestJPopup {

    protected void initUI() {
        JFrame frame = new JFrame(TestJPopup.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTextField textField = new JTextField("Some text field");
        frame.add(textField, BorderLayout.WEST);
        final JButton buttonToHit = new JButton("Hit me");
        buttonToHit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(buttonToHit, "You hit the button successfully");
            }
        });
        frame.add(buttonToHit);
        frame.setSize(200, 100);
        frame.setVisible(true);

        final JPopupMenu popup = new JPopupMenu();
        popup.add(new JLabel("<html>Hey!<br>I'm the popup window!</html>"),
                BorderLayout.NORTH);
        popup.setFocusable(false);
        popup.setVisible(true);
        popup.show(textField, 60, 60);

        // I want to activate popup when user clicks in the text field
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (popup != null) {
                    popup.show(textField, 60, 60);
                }
            }
        });
    }

    public static void main(String[] args) throws Exception {
        Class lnfClass = Class.forName("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", true,
                Thread.currentThread().getContextClassLoader());
        LookAndFeel feel = (LookAndFeel) lnfClass.newInstance();
        UIManager.setLookAndFeel(feel);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestJPopup().initUI();
            }
        });
    }
}

两个关键时刻:

  • 使用的 Windows 外观(默认不可重现)
  • 附加到主框架中文本字段的鼠标侦听器
4

5 回答 5

4

不是答案,而只是一个示例SSCCE,我目前无法在其中重现您描述的行为。也许从这段代码开始,尝试重现错误并使用修改后的非工作代码编辑您的帖子。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestJPopup {

    protected void initUI() {
        JFrame frame = new JFrame(TestJPopup.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel leftLabel = new JLabel("Left");
        frame.add(leftLabel, BorderLayout.WEST);
        final JButton buttonToHit = new JButton("Hit me");
        buttonToHit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(buttonToHit, "You hit the button successfully");
            }
        });
        frame.add(buttonToHit);
        frame.setSize(500, 400);
        frame.setVisible(true);
        JPopupMenu popupMenu = new JPopupMenu();
        popupMenu.add(new JLabel("<html>A Custom<br>component<br>made to<br> simulate <br>your custom component</html>"),
                BorderLayout.NORTH);
        JTextField textfield = new JTextField(30);
        popupMenu.add(textfield);
        popupMenu.setFocusable(false);
        popupMenu.setVisible(true);
        popupMenu.show(leftLabel, 20, 20);
        // Let's force the focus to be in a component in the popupMenu
        textfield.requestFocusInWindow();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestJPopup().initUI();
            }
        });
    }
}
于 2012-09-26T18:12:06.163 回答
2

不是解决方案,但是:

对我来说看起来像一个错误,即使是普通的 componentPopup 也会表现出相同的错误行为(在 winLAF 和 Nimbus 中,而不是在 Metal 中):

JTextField field = new JTextField("some popup owner");
JPopupMenu menu = new JPopupMenu();
menu.add("dummy");
field.setComponentPopupMenu(menu);
Action action = new AbstractAction("hit me!") {

    @Override
    public void actionPerformed(ActionEvent e) {
        LOG.info("got hit!");
    }
};
JComponent content = new JPanel();
content.add(new JButton(action));
content.add(field);
于 2012-09-27T11:38:34.787 回答
2

这是mKorbel在评论中提出的一种可能的解决方法,JWindow而不是:JPopupMenu

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TestJPopup {

    protected void initUI() {
        final JFrame frame = new JFrame(TestJPopup.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTextField textField = new JTextField("Some text field");
        frame.add(textField, BorderLayout.WEST);
        final JButton buttonToHit = new JButton("Hit me");
        buttonToHit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(buttonToHit, "You hit the button successfully");
            }
        });
        frame.add(buttonToHit);
        frame.setSize(200, 70);
        frame.setVisible(true);

        final JWindow popup = new JWindow();
        popup.getContentPane().add(new JLabel("<html>Hey!<br>I'm the popup window!</html>"),
                BorderLayout.NORTH);
        popup.setLocation(frame.getLocation().x + 60, frame.getLocation().y + 60);
        popup.pack();
        popup.setFocusable(false);
        popup.setVisible(true);

        // I want to activate popup when user clicks in the text field
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (popup != null) {
                    popup.setVisible(true);
                    popup.setLocation(frame.getLocation().x + 60, frame.getLocation().y + 60);
                    popup.toFront();
                }
            }
        });

        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (popup != null) {
                    popup.setVisible(false);
                }
            }
        });
    }

    public static void main(String[] args) throws Exception {
        Class lnfClass = Class.forName("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", true,
                Thread.currentThread().getContextClassLoader());
        LookAndFeel feel = (LookAndFeel) lnfClass.newInstance();
        UIManager.setLookAndFeel(feel);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestJPopup().initUI();
            }
        });
    }
}
于 2012-09-28T11:27:28.263 回答
2

为了快速研究和/或未来的读者,

  • 这个问题是可重现的,并提出了,

    一)JPopup

    b) JMenu

  • jdk1.6.0_25在和上测试jdk1.7.0_04

  • WinXp和上的相同问题Win7

  • 对于/ , Look and Feel_SystemLookAndFeelWindowsLookAndFeel

于 2012-09-27T17:29:29.473 回答
0

这是解决问题的神奇线:

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

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

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

顺便说一句,它也发生在 Java 5 和 6 中。

于 2014-02-28T21:28:23.947 回答