我分两部分编写了程序,首先编写了实际的功能,然后编写了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;
    }
}

最佳答案

如果由于某种原因需要以相同的方法打开和处理对话框,则使用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。这很令人困惑

09-10 09:16
查看更多