父对话框状态更改为不可见,但子对话框仍然可见

时间:2015-12-09 15:31:22

标签: java swing window jdialog

在我的项目代码中,我们使用自定义对话框。我正面临一些奇怪的问题。 我有一个孩子对话和一个家长对话。 理想情况下,当我们调用 parent.setVisible(false)时,儿童对话会进入隐身状态。

但我看到一些奇怪的行为。 当我parent.setVisible(false)时,我的孩子对话框仍然可见但是当我试图获得child.isVisible()时:它给了我错误。 此外,当我尝试调用child.setVisible(false)时,它再次对父对话框的可见性没有影响。

注意:由于太多的复杂性,长度和其他外部API问题,我无法显示任何代码示例。此外,我尝试使用外部程序复制它,但它按预期工作,没有看到任何问题。

我只是想知道有人知道在我们制作parent.setVisible(false)时儿童对话框控件放松的任何情况吗?

1 个答案:

答案 0 :(得分:1)

  

我只想知道是否有人知道孩子的任何情况   当我们创建parent.setVisible(false)?

时,对话框控件会松开
  • 对于parent也称为setVisible(false),

  • 请确保您重复使用减少数量的子项,将DefaultCloseOparation设置为HIDE或DISPOSE_ON_CLOSE(通过defasult将最后一个容器关闭,但如果容器之间存在模态则无效)

例如

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author user
 */
public class ModalityAndJDialog {

    private JFrame frame = new JFrame();
    private JDialog dialog1;
    private JDialog dialog2;
    private JButton button = new JButton("Start Swing Timer");
    private JButton button1 = new JButton();
    private JButton button01 = new JButton();
    private JButton button02 = new JButton();
    private Timer timer;

    public ModalityAndJDialog() {
        button.setAction(updateCol());
        frame.setTitle("JFrame");
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocation(150, 150);
        frame.setVisible(true);
        timer = new javax.swing.Timer(500, updateCol());
        timer.setRepeats(false);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Show JDialog") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (dialog1 == null) {
                    dialog1 = new JDialog(dialog1, ModalityType.APPLICATION_MODAL);
                    dialog1.setTitle("1st. JDialog");
                    dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    button1.setAction(updateCol1());
                    button01.setAction(updateCol01());
                    dialog1.add(button1, BorderLayout.SOUTH);
                    dialog1.add(button01, BorderLayout.NORTH);
                    dialog1.pack();
                    dialog1.setSize(400, 300);
                    dialog1.setLocation(250, 250);
                    dialog1.setVisible(true);
                } else {
                    EventQueue.invokeLater(() -> {
                        dialog1.setVisible(true);
                    });
                }
            }
        };
    }

    private Action updateCol01() {
        return new AbstractAction("Hide JDialog") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (dialog1 != null) {
                    dialog1.setVisible(false);
                }
            }
        };
    }

    private Action updateCol1() {
        return new AbstractAction("Show Child JDialog") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (dialog2 == null) {
                    dialog1.setTitle("2nd. JDialog");
                    dialog2 = new JDialog(frame, ModalityType.APPLICATION_MODAL);
                    dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    button02.setAction(updateCol02());
                    dialog2.add(button02, BorderLayout.SOUTH);
                    dialog2.pack();
                    dialog2.setSize(400, 300);
                    dialog2.setLocation(350, 350);
                    dialog2.setVisible(true);
                } else {
                    EventQueue.invokeLater(() -> {
                        dialog2.setVisible(true);
                        if (!frame.isVisible()) {
                            frame.setVisible(true);
                        }
                    });
                }
            }
        };
    }

    private Action updateCol02() {
        return new AbstractAction("Hide JDialog") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (frame != null) {
                    frame.setVisible(false);
                }
                /*if (dialog1 != null) {
                 dialog1.setVisible(false);
                 }*/
            }
        };
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            /*UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());*/
        } catch (UnsupportedLookAndFeelException | ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
            System.out.println("[L&F][Exception] " + ex.getMessage());
        }
        EventQueue.invokeLater(() -> {
            new ModalityAndJDialog();
        });
    }
}
相关问题