public class SimpleWebCrawler extends JFrame {
static JTextArea _resultArea = new JTextArea(200, 200);
JScrollPane scrollingArea = new JScrollPane(_resultArea);
private final static String newline = "\n";
public SimpleWebCrawler() throws MalformedURLException {
_resultArea.setEditable(false);
System.out.println("Please enter the website :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan2.nextLine();
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();
}
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.setContentPane(content);
this.setTitle("Crawled Links");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
}
This class extracts URL from a website and display the output in a JTextArea.
public class Main {
private static void createAndShowGUI() {
JFrame frame1 = new JFrame("FINAL YEAR PROJECT VER 1.0");
JTextArea test = new JTextArea(200, 200);
frame1.setSize(500,500);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(test);
FlowLayout experimentLayout = new FlowLayout();
experimentLayout.setAlignment(FlowLayout.CENTER);
frame1.setLayout(experimentLayout);
JButton button = new JButton("Extract Links");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
开发者_JS百科 {
try {
SimpleWebCrawler.main(null);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame1.getContentPane().add(button);
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
This class is the main class with the GUI. It has a button to call the other class to execute the code. Now the question is, i am putting a JTextArea on this main class frame. How do i transfer the above class outputs to this class JTextArea ?
You have a main inside the constructor in SimpleWebCrawler and another one inside createAndShowGUI in the Main.
You can not have two main methods in one application.
In Java you can not put a method inside another one.
public SimpleWebCrawler() throws MalformedURLException, and you have a try-catch inside as well.
SimpleCrawler will work if you add a closing bracket '}', right before public static void main.
like here...
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
You do not need the Main class at all. The JTextArea is in SimpleWebCrawler. You need to add your logic there.
In order to turn this into a web-crawler you can get the HTML from your _resultArea JTextArea, process it and pass and display the results to a modal JDialog with another JTextArea, or add a second JTextArea next to the one you have in the same JFrame.
精彩评论