【发布时间】:2015-03-23 21:17:31
【问题描述】:
出于调试目的,我需要在屏幕上的一个简单窗口中绘制图像。
Swing 在一个单独的消息循环线程中处理它的所有事件。这意味着如果我执行以下操作:
while(true) {
//Get screenshot
BufferedImage screenshot = MSWindow.screenshot();
//Create JFrame
JFrame frame = new JFrame();
Container main = frame.getContentPane();
//This layout should force the JLabel as large as the window, am I right?
main.setLayout(new GridLayout(1,1));
//Create JLabel to display the screenshot
JLabel label = new JLabel(new ImageIcon(screenshot));
main.add(label);
frame.pack();
frame.setVisible(true);
//Delay is allways good when meddling up with dangerous things
Thread.sleep(2000);
}
...我最终得到了很多很多 JFrame。
我曾经使用 JDialog,它会阻塞并停止线程,直到您按下 OK:
JOptionPane.showMessageDialog(null, scrollPane, message, javax.swing.JOptionPane.INFORMATION_MESSAGE);
这有一个缺陷——你在任务栏上看不到调试窗口。有时,很难找到窗口结束的位置。这就是我想切换到 JFrame 的原因。
我的问题是直截了当的:如何让当前线程等到 JFrame 关闭?
【问题讨论】:
标签: java multithreading swing