JDialog模态问题Java GUI [英] JDialog modal problem java gui

查看:236
本文介绍了JDialog模态问题Java GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,没有任何错误,但是没有按照我的计划运行.我试图弄清楚为什么当我单击标记为重要!"的按钮时,为什么没有显示继续"按钮.

唯一要显示的是空白的弹出窗口,该窗口是代码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);

解决方案

在使对话框可见之前和在pack()对话框之前,您需要将所有组件添加到对话框中. >

之后之后的所有代码dialog.setVisible(true)直到对话框关闭后才执行.

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);

解决方案

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.

这篇关于JDialog模态问题Java GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆