我有一个我正在开发的程序,它应该通过允许您编辑地图中的单个纹理来促进纹理贴图的制作。我有几个地方使用 JDialog 来输入地图的平铺大小、新地图的初始大小,以及一个简单的按钮,用户在完成编辑时按下该按钮使用外部程序(例如 photoshop 或paint)选择纹理。但是,只要这些 JDialog 之一启动,如果程序失去焦点,它就会完全没有响应。这是我在外部编辑选定纹理时弹出的 JDialog 代码:
JDialog editing = new JDialog(mainFrame, "Externally Editing");//mainFrame is the name of the JFrame containing everything.
JPanel pnl = new JPanel();
editing.setLayout(new BorderLayout());
pnl.setPreferredSize(new Dimension(150, 60));
editing.add(pnl);
editing.pack();
JButton button = new JButton("Done Editing");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editng = false;//Obviously, a boolean that I have outside of this method.
}
});
pnl.add(button);
Thread.sleep(100);
editing.setVisible(true);
while(editng){System.out.print("");}//Doing nothing while the user is externally editing.. Unfortunately, if I don't have "System.out.print("");" it doesn't work.. Oh, Java..
new Thread(new Runnable(){public void run(){editing.dispose();}}).start();//Gotta dispose of it in a separate thread since AWT isn't Thread-safe... Ugh..
我会假设它在它为 JDialog 创建/拥有的 AWT 线程中冻结,而我的线程只是在等待按下按钮。这不会发生,因为 JDialog 被冻结。