gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:45:46 25 4
gpt4 key购买 nike

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

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

注意:由于过于复杂、长度和其他外部 API 问题,我无法显示任何代码示例。我还尝试使用外部程序复制它,但它按预期工作,没有发现任何问题。

我只是想知道是否有人知道当我们创建 parent.setVisible(false) 时子对话框控件会松开的情况?

最佳答案

I just want to know does anybody knows any scenario in which a child dialog control get loosen up when we make parent.setVisible(false)?

  • 父级也称为 setVisible(false),

  • 确保重复使用减少数量的子项,将 DefaultCloseOparation 设置为 HIDE 或 DISPOSE_ON_CLOSE(默认情况下最后一个容器关闭灯,但如果容器之间存在模态则不会产生影响)

例如

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

关于java - 父对话框状态更改为不可见,但子对话框仍然保持可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com