gpt4 book ai didi

java - 为什么在 JFrame 上显示 JOptionPane 时我的应用程序会卡住?

转载 作者:行者123 更新时间:2023-12-01 14:06:24 26 4
gpt4 key购买 nike

我在 JFrame 窗口中编写了一个应用程序,并且希望在需要时弹出错误消息。但是,当我调用“JOptionPane.showMessageDialog()”时,应用程序卡住,停止它的唯一方法是使用任务管理器。
这是我的代码的精简版本:

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.JFrame;

public class GameMain {
public JFrame jframe;
public Canvas canvas;

private AtomicReference<Dimension> canvasSize = new AtomicReference<Dimension>();

public void initialize(int width, int height) {
try {
Canvas canvas = new Canvas();
JFrame frame = new JFrame("testapp");
this.canvas = canvas;
this.jframe = frame;
ComponentAdapter adapter = new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
resize();
}
};

canvas.addComponentListener(adapter);
canvas.setIgnoreRepaint(true);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setVisible(true);
Dimension dim = this.canvas.getSize();
} catch (LWJGLException le) {
le.printStackTrace();
}

JOptionPane.showMessageDialog(null, "oops!");
}
public void resize()
{
Dimension dim = this.canvas.getSize();
canvasSize.set(dim);
dim = null;
}
}

有谁知道它为什么会这样做?

最佳答案

private void ShowMessage(String message) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, message);
}
});
}

关于java - 为什么在 JFrame 上显示 JOptionPane 时我的应用程序会卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503798/

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