gpt4 book ai didi

swing - 使用 openjdk 1.7.0_101 的 java7 swing 中的潜在错误

转载 作者:行者123 更新时间:2023-12-01 09:36:00 25 4
gpt4 key购买 nike

当使用 JDialog 的 dispose() 方法时,我会在第一个 JDialog 关闭时收到两个 windowClosed 事件,而对于后续 JDialog,我会为每个之前关闭的 JDialog 收到一个额外的关闭事件(加上新 JDialog 的另外两个)。

我的代码(下面发布)有问题吗?

package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;

public class Main
{

public static void main(String[] args) throws InterruptedException
{
JDialog dialog;
while (true)
{
dialog = createDialog();
while (dialog.isVisible())
{
Thread.sleep(10);
}
}
}

public static JDialog createDialog()
{
final JDialog dialog = new JDialog();

JButton close = new JButton("Close");
dialog.add(close);

final WindowListener windowListener = new WindowListener() {

@Override
public void windowOpened(WindowEvent e) {
System.err.println("windowOpened " + dialog.hashCode());
}

@Override
public void windowIconified(WindowEvent e) {
System.err.println("windowIconified " + dialog.hashCode());
}

@Override
public void windowDeiconified(WindowEvent e) {
System.err.println("windowDeiconified " + dialog.hashCode());
}

@Override
public void windowDeactivated(WindowEvent e) {
System.err.println("windowDeactivated " + dialog.hashCode());
}

@Override
public void windowClosing(WindowEvent e) {
System.err.println("windowClosing " + dialog.hashCode());
}

@Override
public void windowClosed(WindowEvent e) {
System.err.println("windowClosed " + dialog.hashCode());

// uncommenting this provides a fix to the bug
// dialog.removeWindowListener(this);
}

@Override
public void windowActivated(WindowEvent e) {
System.err.println("windowActivated " + dialog.hashCode());
}
};

close.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});

dialog.addWindowListener(windowListener);

dialog.pack();
dialog.setVisible(true);
return dialog;
}
}

关闭一个 JDialog 框后此代码的示例输出:

windowOpened 1479616934
windowActivated 1479616934
windowDeactivated 1479616934
windowClosed 1479616934
windowClosed 1479616934
windowOpened 1514973352
windowActivated 1514973352

关闭第二个 JDialog 框后的示例输出:

windowClosed 1514973352
windowClosed 1479616934
windowClosed 1514973352
windowOpened 78938440
windowActivated 78938440
  • 受影响的版本:OpenJDK 1.7.0_101,但不包括 OpenJDK 1.8.0_91
  • 受 JRE 影响,但不受 Javac 影响

最佳答案

对话框将链接到其所有者,如果在创建对话框时未指定所有者,这可能会影响对话框的行为,如您所见。最好始终将您的对话框与正确的所有者(或精心设计的空值)相关联,以获得最佳行为。

关于swing - 使用 openjdk 1.7.0_101 的 java7 swing 中的潜在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38923234/

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