JDialog模态问题java gui

发布于 2024-10-10 11:45:36 字数 1408 浏览 3 评论 0原文

我的代码运行时没有任何错误,但它没有按我计划的方式运行。我试图找出为什么当我单击标有“重要!!”的按钮时没有显示“继续”按钮。

唯一显示的是一个空白的弹出窗口,它是代码 JDialog 的一部分,并且设置为模态且可见。我就是想不通。如果有人可以帮助我,我将非常感激。

JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        final JPanel hehePanel = new JPanel();
        final JDialog dialog = new JDialog();
        dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
        dialog.toFront();
        dialog.setModal(true);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        JButton closebutton = new JButton("Continue");
        closebutton.setActionCommand("continue");
        closebutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (evt.getActionCommand().equals("continue")) {
                    dialog.dispose();
                }
            }
        });
        hehePanel.add(closebutton);
    }
});

hehePanel.add(fButton);
hehePanel.add(exitButton);

I have this code which runs without any errors but it isn't running the way I planned. I'm trying to figure out why my "Continue" button is not being displayed when I click the button labeled "important!!".

The only thing which is being displayed is a blank pop up window which is part of the code JDialog and it set to modal and visible. I just can't figure it out. If anyone can help me I would appreciate it a lot.

JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        final JPanel hehePanel = new JPanel();
        final JDialog dialog = new JDialog();
        dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
        dialog.toFront();
        dialog.setModal(true);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        JButton closebutton = new JButton("Continue");
        closebutton.setActionCommand("continue");
        closebutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (evt.getActionCommand().equals("continue")) {
                    dialog.dispose();
                }
            }
        });
        hehePanel.add(closebutton);
    }
});

hehePanel.add(fButton);
hehePanel.add(exitButton);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-10-17 11:45:36

您需要在使对话框可见之前以及 pack() 对话框之前将所有组件添加到对话框中。

dialog.setVisible(true) 之后的所有代码在对话框关闭之前不会执行。

You need to add all the components to the dialog before making the dialog visible and before you pack() the dialog.

All the code after dialog.setVisible(true) does not execute until the dialog is closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文