简体   繁体   English

如何暂停程序的执行,直到在Java Swing中单击按钮为止

[英]How to pause execution of a program until a button is clicked in Java Swing

I have a program with a GUI that needs to open a separate window and wait for the user to select and option, then continue. 我有一个带有GUI的程序,需要打开一个单独的窗口并等待用户选择和选择,然后继续。 I figure I should be doing this with the wait() and notify() methods, but I'm still trying to figure out exactly how to use those. 我认为我应该使用wait()和notify()方法来执行此操作,但是我仍在尝试弄清楚如何使用它们。 A complicating factor is that things seem to work differently when the second window is created in an actionPerformed() method, which it needs to be. 一个复杂的因素是,当第二个窗口在actionPerformed()方法中创建时,事情似乎有所不同。

Here's how I think it should be done here, apparently it is not quite right... 我认为这是应该在这里完成的方式,显然这不太正确...

This should create a window with a button, when the button is pressed, another window with a button should be created, and when that button is pressed, the program should print "End". 这应该创建一个带有按钮的窗口,当按下按钮时,应该创建另一个带有按钮的窗口,并且当按下该按钮时,程序应该打印“ End”。

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class WtfExample {

 public static void main(String[] args) {
  JFrame jf = new JFrame();
  JButton butt = new JButton("Button");
  butt.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {
    WtfExample we = new WtfExample();
    we.display();
   }
  });

  jf.getContentPane().add(butt);
  jf.setSize(new Dimension(1000, 500));
  jf.setVisible(true);

  System.out.println("End");
 }

 public synchronized void display() {
  JFrame jf = new JFrame();

  JButton butt = new JButton("Button");
  butt.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent e) {
    synchronized(WtfExample.this) {
     WtfExample.this.notifyAll();
    }
   }
  });

  jf.getContentPane().add(butt);
  jf.setSize(new Dimension(1000, 500));
  jf.setVisible(true);

  while(true) {
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
    break;
   }
  }
 }
}

edit- I wasn't clear enough in one thing- the second window that's opened is blank, like its components were never added to it. 编辑-一件事我还不够清楚-打开的第二个窗口是空白的,就像它的组件从未添加过一样。 That's the case whether it's a frame or dialog, but that only happens if the window is created from the actionPerformed method. 不管是框架还是对话框,都只有这种情况,但是只有在使用actionPerformed方法创建窗口时才会发生这种情况。

不,您应该只使用JDialog

You need a modal dialog window. 您需要一个模式对话框窗口。 Here 'sa tutorial on dialogs. 这里是对话框的教程。 It is easier to use JOptionPane for the simple cases. 对于简单情况,使用JOptionPane会更容易。

A Dialog can be modal. 对话框可以是模式对话框。 When a modal Dialog is visible, it blocks user input to all other windows in the program. 当模式对话框可见时,它将阻止用户输入程序中的所有其他窗口。

As the other two answers suggest you need a modal JDialog. 正如其他两个答案所示,您需要一个模式JDialog。 You do not need to deal with any Thread classes. 您不需要处理任何线程类。 The JDialog window will deal with the giving you control back once the user input is handled. 一旦处理了用户输入,JDialog窗口将处理您的控制权。 There are a few ways you can set the dialog box modal. 您可以通过几种方法来设置对话框模式。 Here are two examples. 这是两个例子。

new JDialog(Dialog owner, boolean modal)

or 要么

new JDialog(Dialog owner, String title, boolean modal)

You could also do something like this: 您还可以执行以下操作:

JDialog dialog = new JDialog(owner);
dialog.setModal(true);

I think this is a pretty good article about modality in JAVA. 我认为是一篇有关JAVA模态的不错的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM