java - 创建一个检查属性窗口,按钮作为 JDialog 驱动

标签 java swing jbutton jdialog jpopupmenu

我最初问的没有明确说明我的问题/问题,所以我会更好地解释它。我有一个将 JDialog 设置为可见的 JButton。 JDialog 有一个 WindowListener 将其设置为在 windowDeactivated() 事件中不可见,该事件在用户单击对话框外部时触发。按钮 ActionListener 检查对话框是否可见,如果为真则隐藏它,如果为假则显示它。

windowDeactivated() 无论是否点击按钮都会触发,只要用户在对话框外点击。我遇到的问题是当用户单击按钮关闭对话框时。对话框由 WindowListener 关闭,然后 ActionListener 尝试显示它。

如果 windowDeactivated() 没有 setVisible(false),则对话框仍然打开,但在父窗口后面。我要问的是如何访问 windowDeactivated() 中点击的位置。如果我知道用户点击了按钮并且 windowDeactivated() 可以跳过隐藏对话框,这样按钮的 ActionListener 就会看到它仍然可见并隐藏它。

public PropertiesButton extends JButton {

    private JDialog theWindow;

    public PropertiesButton() {
        theWindow = new JDialog();
        theWindow.setUndecorated(true);
        theWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        theWindow.add(new JMenuCheckBoxItem("Something"));
        theWindow.addWindowListener(new WindowListener() {
            // just an example, need to implement other methods
            public void windowDeactivated(WindowEvent e) {
                theWindow.setVisible(false);
            }
        });
        this.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (theWindow.isVisible()) {
                    theWindow.setVisible(false);
                } else {
                    JButton btn = (JButton)e.getSource();
                    theWindow.setLocation(btn.getLocationOnScreen.x,btn.getLocationOnScreen.x-50);
                    theWindow.setVisible(true);
                }
            }
        });
        theWindow.setVisible(false);
    }

}

最佳答案

您可以尝试对下拉属性列表使用 JPanel 而不是 JDialog。像这样:

public class PropertiesButton extends JButton {

    private JPanel theWindow;

    public PropertiesButton() {
        theWindow = new JPanel();
        theWindow.add(new JMenuCheckBoxItem("Something"));

        this.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (theWindow.isVisible()) {
                    theWindow.setVisible(false);
                    getParent().remove(theWindow);
                } else {
                    JButton btn = (JButton)e.getSource();
                    getParent().add(theWindow);             
                    theWindow.setBounds(
                       btn.getX(),
                       btn.getY() + btn.getHeight(), 100, 100);

                    theWindow.setVisible(true);
                }
            }
        });
        theWindow.setVisible(false);
    }

}

在 Swing 中使用轻量级组件而不是像 JDialog 这样的重量级组件总是可取的,并且像您报告的那样具有较少的不良影响。这种方法的唯一问题是面板的位置和大小可能会受到父级中 Activity 的布局管理器的影响。

关于java - 创建一个检查属性窗口,按钮作为 JDialog 驱动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4195395/

相关文章:

java - 当可以使用 static{} 时为什么还要使用 main() ?

c# - WPF 应用程序中的 Java Swing Windows "always on top"

java - 为什么我的 JButton 总是在 GUI 上弹出?

java - Action/Keylisteners 不能正常工作

java - 按钮大小 (Java)

java - string.split() 方法的输出

java - 如何在java中键盘输入后更改图像?

java - 编辑条目后 ListView 滚动到顶部

java - SwingWorker Process() 警告

java - 当鼠标使用 Graphics2D 移动时,如何显示 X 和 Y 鼠标位置?