2013-07-23 97 views
1

我编写了我的程序分为两部分,我先写了实际的功能,然后再将GUI显示出来。 我需要等待/暂停用户在继续执行代码之前单击另一个类(DisplayImages)中的“完成”按钮。 我的DisplayImages类获取MyImage的列表。然后图像显示在jpanel中,用户选择一对图像 ,然后单击“完成”按钮。我如何等待回应或类似的东西?java暂停代码并等待用户输入

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

回答

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接口和/或使用线程。这很混乱

+0

+1当然,听者比我的建议好,但我想保持容易。 – Stephan

+0

这看起来像我可能会用到的东西。我可能必须使用反射,因为公共无效运行(最后一个回调)硬编码只接受类型一类,但我需要其他类调用运行。 – altoids

+0

@Miss_poker你可以让所有的类像'One'实现一个接口(让我们来说'Caller'),它有一个方法'public void processSelectedImages(List selectedImages)''。然后,'DisplayImages.run()'将变为'public void run(最终的调用者调用者)'。如果这还不够好,请编辑您的问题,以提供有关您的限制的说明 –

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