1

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

显示图像类

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;
    }
}
4

3 回答 3

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接口和/或使用线程,请不要命名您的方法。这很令人困惑

于 2013-07-23T08:37:20.227 回答
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;
    }
}
于 2013-07-23T08:00:25.030 回答
-1

您将不得不使用线程。

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

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

或者让自己有一个全局标志变量,静态的,对两个类都可用,您更改它以通知另一个类单击了完成按钮。

于 2013-07-23T07:48:57.437 回答