java暂停代码并等待用户输入

时间:2013-07-23 07:34:58

标签: java multithreading jframe jpanel

我的程序分两部分编写,我首先编写了实际的功能,然后是GUI来显示它。 在继续执行代码之前,我需要等待/暂停用户从另一个类(DisplayImages)单击“完成”按钮。 我的DisplayImages类获取了一个MyImage列表。然后,图像显示在jpanel中,用户选择几个图像 然后单击“完成”按钮。我怎么能等待回复或类似的事情?

public class One{

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One(){
        DisplayImages displayOne = new DisplayImages(images);
        displayOne.run();
        //I need to pause/wait here until the user has pressed the done button
        //in the DisplayImages class

        images.clear();
        images = displayOne.getSelectedImages();

        //do stuff here with images arrylist
        }
}    

DisplayImages类

public class DisplayImages extends JFrame{
    private ArrayList<MyImage> images = new ArrayList<MyImage>();
    private ArrayList<MyImage> selectedImages = new ArrayList<MyImage>();

    public DisplayImages(ArrayList<MyImage> images){
        this.images = images;
    }

    public void run(){
        //code creates a jpanel and displays images along with a done button

        //user presses done button
        done.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                selectedImages = getSelectedImages();
                //here I would send a signal to continue running in class One
            }
        });
    }

    private ArrayList<MyImage> getSelectedImages(){
        //code returns list of all the images the user selected on JPanel
        return results;
    }
}

3 个答案:

答案 0 :(得分:2)

如果出于某种原因需要以相同的方法打开并处理对话框,那么使用JOptionPane建议的对话框方法似乎很好。然而,这似乎是糟糕的设计(打开一个框架并在构造函数中等待输入?)。我更喜欢类似下面的方法(请阅读我的内联评论):

public class One {

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One() {
        // perform only initialization here
    }

    // call this method to create the dialog that allows the user to select images
    public void showDialog() {
        DisplayImages displayOne = new DisplayImages(images);

        // pass a reference to this object so DisplayImages can call it back
        displayOne.run(this);
    }

    // this will be called by the action listener of DisplayImages when Done is clicked
    public void processSelectedImages(List<MyImage> selectedImages) {
        images.clear();
        images = selectedImages;

        // do stuff here with images arrylist
    }
}


public class DisplayImages {
    ...
    public void run(final One callback){  // Note that now a reference to the caller is passed
         // creates jpanel and displays images along with a done button

         // user presses done button
         done.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 setVisible(false);
                 selectedImages = getSelectedImages();

                 // here is how we can send a signal to notify the caller
                 callback.processSelectedImages(selectedImages); 
             }
         });
    }
    ...
}

作为旁注,如果您没有实现run()接口和/或使用线程,请不要将方法命名为Runnable。这非常令人困惑

答案 1 :(得分:1)

这很容易,这里的一些人正在考虑复杂化。您不需要多线程,只需要一个模态对话框。 JOptionPane可让您轻松访问它们。

我修改了你的代码:

public class One{

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One(){
        DisplayImages displayOne = new DisplayImages(images);
        int n = JOptionPane.showConfirmDialog(null, displayOne);

        if (n == JOptionPane.OK_OPTION){
          //I need to pause/wait here until the user has pressed the done button
          //in the DisplayImages class

          images = displayOne.getSelectedImages();

          //do stuff here with images arrylist
          }
        }
}    

MyImage类

public class DisplayImages extends JPanel{
    private ArrayList<MyImage> images = new ArrayList<MyImage>();

    public DisplayImages(ArrayList<MyImage> images){
        this.images = images;

        //code creates a jpanel and displays images along with a done button
    }

    public ArrayList<MyImage> getSelectedImages(){
        //code returns list of all the images the user selected on JPanel
        return results;
    }
}

答案 2 :(得分:-1)

您必须使用线程。

http://docs.oracle.com/javase/tutorial/essential/concurrency/

然后,您可以在单独的线程上设置每个实例,并使用内置的Object.notify and Object.wait

或者让自己拥有一个全局标志变量static,可用于两个类,您可以更改这些变量以通知另一个类已单击完成按钮。