我已经尝试过,尝试过并尝试过失败。我已经查看了有关SO的所有相关问题,没有什么对我有用!
我有一个从另一个类调用的方法。
public void generateObstacles () throws IOException {
new Thread(new Runnable() {
public void run() {
Obstacles.NeedyDog windowPet = null;
try
{
windowPet = new NeedyDog();
}
catch ( IOException e )
{
e.printStackTrace();
}
windowPet.setBounds( 100, 100, 1000, 400);
windowPet.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
windowPet.setResizable(false);
windowPet.setVisible( true );
windowPet.pack();
windowPet.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {}
});
}
}).start();
System.out.println( "Don't move on until it closes" );
}
我需要用户使用NeedyDog的JDialog框架,并在generateObstacles方法移动到print语句之前进行处理,但它会立即移动并将其打印到控制台上。
这是我的NeedyDog课程:
private class NeedyDog extends JDialog implements ActionListener {
public NeedyDog() throws IOException
super((Window)null);
setModal(true);
// Code to setup the JDialog
}
@Override
public void actionPerformed( ActionEvent e )
{
final JFrame window = (JFrame)SwingUtilities.getRoot(this);
if (e.getActionCommand().equals("buyball")) {
user.addCoins( -3 );
dogTalk.setText(dogTalk.getText() + "\nVery nice. Now I want "
+ "yeh teh play with me!!");
ball.removeActionListener(this);
ball.setText("Oops. Try buying something else.");
}
else if (e.getActionCommand().equals("buybone"))
{
user.addCoins( -5 );
dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much"
+ " thanks bye.");
if (dogTalk.getText().contains("Very nice."))
{
window.setTitle( "Obstacle overcome!-Dog is happy with"
+ " a ball AND bone! Closing window...");
}
else
{
window.setTitle( "Obstacle overcome!-Dog is happy!"
+ " Closing window...");
}
new Thread(new Runnable() {
public void run() {
try
{
//Wait 5 seconds before exiting
TimeUnit.SECONDS.sleep(5);
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
window.dispose();
}
}).start();
}
}
答案 0 :(得分:3)
解决方案很简单:不要使用JFrame,而是使用模态 JDialog,因为这种行为正是为模式对话框构建的。
关于:
我已经尝试过,尝试过并尝试过失败。我已经查看了有关SO的所有相关问题,没有什么对我有用!
这个问题已被多次询问,大多数答案都采用相同或类似的解决方案。 For example
另请查看How to use Dialogs。
如果您需要更具体的代码帮助,请尝试将代码压缩到仍然编译和运行的最小位,没有与您的问题无关的额外代码,但仍然可以证明您的问题,换句话说, minimal example program