[英]java Pause code and wait for user input
I wrote my program in two parts, I wrote the actual functionality first, and then the GUI to display it all. 我分两部分编写了程序,首先编写了实际的功能,然后编写了GUI来显示所有功能。 I need to wait/pause for a user to click a "Done" button from another class(DisplayImages) before continuing executing code. 我需要等待/暂停,以便用户在继续执行代码之前从另一个类(DisplayImages)单击“完成”按钮。 My DisplayImages class takes a list of MyImage. 我的DisplayImages类采用MyImage的列表。 The images are then displayed in a jpanel and the user selects a couple of images and then clicks a 'Done' button. 这些图像然后显示在jpanel中,用户选择几张图像,然后单击“完成”按钮。 How can I wait for a response or something like that? 如何等待回复或类似的回复?
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 class 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;
}
}
If for some reason you need to open and process the dialog in the same method then using the dialog approach suggested with JOptionPane
seems fine. 如果由于某种原因需要以相同的方法打开和处理对话框,则使用JOptionPane
建议的对话框方法似乎很好。 However this seems bad design (opening a frame and waiting for input in a constructor?). 但是,这似乎是不好的设计(打开框架并等待构造函数中的输入?)。 I would prefer an approach similar to the one below (please read my inline comments): 我希望采用与以下方法类似的方法(请阅读我的内联评论):
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);
}
});
}
...
}
As a side note please do not name your methods run()
if you are not implementing the Runnable
interface and/or using threads. 附带说明一下,如果您未实现Runnable
接口和/或使用线程,请不要为您的方法run()
命名。 This is very confusing 这很令人困惑
That is quite easy, some people here are thinking to complicated. 这很容易,这里有些人正在想复杂化。 You will need no multithreading, you just need a modal dialog. 您将不需要多线程,只需要一个模式对话框。 JOptionPane provides easy access to them. JOptionPane提供了对它们的轻松访问。
I modified your code: 我修改了您的代码:
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 class 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;
}
}
You will have to use threading. 您将必须使用线程。
http://docs.oracle.com/javase/tutorial/essential/concurrency/ http://docs.oracle.com/javase/tutorial/essential/concurrency/
Then you can set up each instance on a separate thread, and either use the built in Object.notify and Object.wait
然后,您可以在单独的线程上设置每个实例,并使用内置的Object.notify and Object.wait
Or have yourself a global flag variable, static, available to both classes, which you change to notify the other class that the done button was clicked. 或者让自己为两个类都可用的全局标志变量static,您可以更改该变量以通知另一类已单击完成按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.