等待并通知等待按钮按下导致jframe未加载



我正在编写一个方法(getInfo),由我的程序的主类调用。在一种情况下,用户需要从列表(在JComboBox中)中进行选择,以确定方法返回的内容,因此我希望该方法等待,直到用户做出选择。我正在尝试使用wait和notify来做到这一点,但是当jframe弹出时,其中包含用户进行选择的JComboBox,它是空的。我怀疑这是因为调用wait()暂停了我的整个程序,所以我怎么能等待按钮被按下来完成方法并让它返回一些东西呢?下面是有问题的类:

package sm;
import org.jsoup.Jsoup;
//import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ScrapingManager {

String courseName;
String courseType;
String site;
Element row;
Elements allWithName;
JComboBox<String> options;
JFrame frame;
String slots;
String location;
public static final Object obj = new Object();
public ScrapingManager(String name) {
courseName = name;
try {
courseType = name.substring(0, 4);
} catch(Exception e) {System.out.println("Error: Course code must be at least 4 characters");};
//getInfo(searchFirstPage());
}

public String searchFirstPage() {
Document page;
String result = "error";
try {
page = Jsoup.connect("https://app.stfx.ca/web/Forms/shared/Registrar_website_Timetable.htm").get();
Element link = page.getElementsContainingOwnText(courseType).first();
//System.out.println(link.attr("href"));
result = link.attr("href");
} catch(Exception e) {/*e.printStackTrace();*/System.out.println("Error: Course code not recognized");}
return result;
}

public String[] getInfo() { 
Document doc;
slots = "Error";
location = "Error";
try {
doc = Jsoup.connect(searchFirstPage()).get();
allWithName = doc.getElementsContainingOwnText(courseName).select(":not(:contains('L'))");//it works without select part but shows labs too
if(allWithName.size() > 1) {
//give warning that it might be the wrong times
frame = new JFrame("Warning");
JPanel panel = new JPanel();
JLabel warning1 = new JLabel("There are multiple classes with that name,");
JLabel warning2 = new JLabel("select yours from the list below:");
String[] array = new String[allWithName.size()];
for(int i = 0; i < allWithName.size(); i++) {
array[i] = allWithName.get(i).parent().child(8).text();
}
options = new JComboBox<String>(array);
JButton ok = new JButton("Select");
ok.addActionListener(new SubmitListener());
panel.add(warning1);
panel.add(warning2);
panel.add(options);
panel.add(ok);
frame.getContentPane().add(panel);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
synchronized(obj) {
try {
obj.wait();
} catch(Exception e) {}
}
} else {
row = doc.getElementsContainingOwnText(courseName).first().parent();
}

slots = row.child(8).text();
location = row.child(9).text();
//System.out.println(slots + " " + location);
} catch(Exception e) {e.printStackTrace();}
return new String[] {slots, location};
}

public class SubmitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {    
row = allWithName.get(options.getSelectedIndex());
frame.dispose();
synchronized(obj) {
obj.notify();
}
}    
}
}

提前感谢!

JOptionPane.showInputDialog(...)代替wait(),如下所示:

import javax.swing.JOptionPane;
public class LanguagesSelector{
public static void main(String[] args) {
String[] languages = {"Spanish", "English", "Italian", "Other"};
Object language = JOptionPane.showInputDialog(null,
"Choose Language", "language", JOptionPane.PLAIN_MESSAGE,
null, languages, languages[0]);
System.out.println("Selected " + language);
}
}

JOptionPane类中,AWT (swing的基础)线程等待用户选择一个选项,所以它应该在您的问题中工作。

最新更新