为了进行调试,我需要在一个简单的窗口中在屏幕上绘制图像.
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关闭?