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);
}
}
此类从网站中提取URL并在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)
{
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();
}
});
}
}
此类是具有GUI的主类。它有一个按钮来调用另一个类来执行代码。现在的问题是,我在这个主类框架上放置一个JTextArea。如何将上述类输出传输到此类JTextArea?
答案 0 :(得分:0)
您在SimpleWebCrawler中的构造函数中有一个main,而Main中的另一个在createAndShowGUI中。
在一个应用程序中不能有两个主要方法。
在Java中,您无法将方法放在另一个方法中。
public SimpleWebCrawler()抛出MalformedURLException,你也有一个try-catch。
如果在public static void main之前添加一个右括号'}',SimpleCrawler将会起作用。
喜欢这里......
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
public static void main(String[] args) throws IOException {
JFrame win = new SimpleWebCrawler();
win.setVisible(true);
}
根本不需要Main类。 JTextArea位于SimpleWebCrawler中。你需要在那里添加你的逻辑。
为了将其转换为网络爬虫,您可以从_resultArea JTextArea获取HTML,处理它并将结果传递给具有另一个JTextArea的模态JDialog,或者在您拥有的那个旁边添加第二个JTextArea在同一个JFrame中。