import org.jsoup.Jsoup;
@SuppressWarnings({ "unused", "serial" })
public class SimpleWebCrawler extends JFrame {
JTextField yourInputField = new JTextField(20);
static JTextArea _resultArea = new JTextArea(200, 200);
JScrollPane scrollingArea = new JScrollPane(_resultArea);
private final static String newline = "\n";
String word2;
public SimpleWebCrawler() throws MalformedURLException {
yourInputField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
word2 = yourInputField.getText();
}
});
_resultArea.setEditable(false);
try {
URL my_url = new URL("http://" + word2 + "/");
BufferedReader br = new BufferedReader(new InputStreamReader(
my_url.openStream()));
String strTemp = "";
while (null != (strTemp = br.readLine())) {
_resultArea.append(strTemp + newline);
}
} catch (Exception ex) {
ex.printStackTrace();
}
_resultArea.append("\n");
_resultArea.append("\n");
_resultArea.append("\n");
String url = "http://" + word2 + "/";
print("Fetching %s...", url);
try{
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
System.out.println("\n");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
_resultArea.append("\n");
for (Element link : links) {
print(" %s ", link.attr("abs:href"), trim(link.text(), 35));
bw.write(link.attr("abs:href"));
bw.write(System.getProperty("line.separator"));
}
bw.flush();
bw.close();
} catch (IOException e1) {
}
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
content.add(yourInputField,BorderLayout.SOUTH);
this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
JPanel content2 = new JPanel();
this.setContentPane(content2);
this.setTitle("Input the URL");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
private static void print(String msg, Object... args) {
_resultArea.append(String.format(msg, args) +newline);
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width - 1) + ".";
else
return s;
}
//.. Get the content pane, set layout, add to center
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
}
我试图创建一个JTextField接受用户输入。输入将转到此代码行以处理代码。
URL my_url = new URL("http://" + word2 + "/");
String url = "http://" + word2 + "/";
但是,运行该代码时不会要求用户输入。 JTextField没有出现,我直接得到一个错误,因为我在din输入输入。
我试图让JTextField接受来自用户的输入。但是,它不会出现,并且代码将直接进行处理,最后以空的my_url和rmpty url变量结束。
如何根据我发布的代码创建JTextField?我创建的Jtextfield似乎与我的代码发生冲突。
最佳答案
Java swing不遵循命令式方法,而是事件驱动的。您的构造方法完全执行,并且不等待您的输入。
您不得将业务逻辑(即所有这些读/写内容)包含在此方法中,而应包含在单独的方法中,并从您在输入字段中注册的操作侦听器中调用它。 (例如,参见http://download.oracle.com/javase/tutorial/uiswing/events/index.html)
注意A:如果您的逻辑很繁重,则应生成一个后台线程,而不应直接在操作侦听器中执行它(请参见Swingworker)。
注意B:您的课程中有很多奇怪的代码。
this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
JPanel content2 = new JPanel();
this.setContentPane(content2);
this.setTitle("Input the URL");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
如前所述,它将立即运行,因此您的面板
content
根本不会显示,因为它会被content2
覆盖。