我想用java寫一個簡單的網頁瀏覽器,這是我的代碼!瀏覽javascript
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class WebBrowser extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JPanel
address_panel, window_panel;
public JLabel
address_label;
public JTextField
address_tf;
public JEditorPane
window_pane;
public JScrollPane
window_scroll;
public JButton
address_b;
private Go go = new Go();
public WebBrowser() throws IOException {
// Define address bar
address_label = new JLabel(" address: ", SwingConstants.CENTER);
address_tf = new JTextField("http://www.yahoo.com");
address_tf.addActionListener(go);
address_b = new JButton("Go");
address_b.addActionListener(go);
window_pane = new JEditorPane("http://www.yahoo.com");
window_pane.setContentType("text/html");
window_pane.setEditable(false);
address_panel = new JPanel(new BorderLayout());
window_panel = new JPanel(new BorderLayout());
address_panel.add(address_label, BorderLayout.WEST);
address_panel.add(address_tf, BorderLayout.CENTER);
address_panel.add(address_b, BorderLayout.EAST);
window_scroll = new JScrollPane(window_pane);
window_panel.add(window_scroll);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(address_panel, BorderLayout.NORTH);
pane.add(window_panel, BorderLayout.CENTER);
setTitle("web browser");
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class Go implements ActionListener{
public void actionPerformed(ActionEvent ae){
try {
window_pane.setPage(address_tf.getText());
} catch (MalformedURLException e) { // new URL() failed
window_pane.setText("MalformedURLException: " + e);
} catch (IOException e) { // openConnection() failed
window_pane.setText("IOException: " + e);
}
}
}
public static void main(String args[]) throws IOException {
WebBrowser wb = new WebBrowser();
}
}
它適用於簡單的html頁面,但它不能加載JavaScript代碼的一部分!我的問題是我應該添加到代碼來加載javascripts?謝謝!
您需要將某種Javascript引擎放入您的瀏覽器。你可以編寫你自己的,或者環顧四周,看看是否有人把它移植到Java。 – Makoto
*「我想寫一個簡單的網絡瀏覽器」*我想爲我的寵物農場釣到Dodos。鑑於沒有Dodos,也沒有「簡單的網絡瀏覽器」,我們都將我們的工作剪掉了。 ;) –
你能解釋一下嗎? –