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);
}
}
I am tryi开发者_运维技巧ng to create a JTextField to accept the user input. The input will go to this line of code to process the code.
URL my_url = new URL("http://" + word2 + "/");
String url = "http://" + word2 + "/";
However, the code is run without asking the user for input. The JTextField does not appear and i straight get an error on because i din enter the input.
I am trying to get the JTextField to accept input from the user. However, it does not appear and the code straight proceed with the processing end up with empty my_url and rmpty url variable.
How do i create a JTextField according to my code that i post ? It seems that the Jtextfield i created clashed with my codes.
Java swing does not follow the imperative approach but is event driven. Your constructor method is executed in total and does not wait for your input.
You must not include the business logic (i.e. all this read/write stuff) into this method but into a separate one and invoke it from the action listener your registered with your input field. (see e.g. http://download.oracle.com/javase/tutorial/uiswing/events/index.html)
Note A: If your logic is quite heavy you should spawn a background thread and not do it directly in your action listener (see Swingworker).
Note B: There is lot's of strange code within your class.
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();
As mentioned before this will be run immediately and thus your panel content
is never shown at all because it will be overwritten by content2
.
精彩评论