[英]Javascript working in Java web browser?
這可能嗎? 我做了一個簡單的網絡瀏覽器,它可以很好地查看一些頁面,但是大多數頁面都弄亂了,我相信可能是因為這個原因(未啟用javascript)。 例如,CNN.com的顯示方式如下: http : //www.glowfoto.com/static_image/07-181402L/5111/png/07/2010/img5/glowfoto
到目前為止,這是我的代碼:
public class Browser extends JPanel {
private JEditorPane mainArea;
private JTextField adress;
private JPanel justtoseparatethings;
public Browser() {
setLayout(new BorderLayout());
setVisible(true);
mainArea = new JEditorPane();
adress = new JTextField();
justtoseparatethings = new JPanel();
adress.setEditable(true);
adress.setSize(0, 0);
mainArea.setEditable(false);
mainArea.setContentType("text/html");
justtoseparatethings.setLayout(new BorderLayout());
justtoseparatethings.add(adress);
add(justtoseparatethings, BorderLayout.NORTH);
//mejor separar estos por paneles tb
//add(mainArea, BorderLayout.CENTER);
add(new JScrollPane(mainArea),BorderLayout.CENTER);
adress.addKeyListener(new KeyListener() {
public void keyReleased(KeyEvent ke) {
}
public void keyPressed(KeyEvent ke) {
if (adress.hasFocus() && ke.getKeyCode() == KeyEvent.VK_ENTER) {
try {
String unproc = adress.getText();
String start = "http://";
int index1 = unproc.indexOf(start);
if (index1 != -1) {
mainArea.setPage(adress.getText());
} else {
unproc = start + unproc;
}
adress.setText(unproc);
mainArea.setPage(unproc);
} catch (Exception a) {
System.out.println("there was an error in your request");
}
}
}
public void keyTyped(KeyEvent ke) {
}
});
mainArea.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (hle.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
mainArea.setPage(hle.getURL());
adress.setText(mainArea.getPage().toString());
} catch (Exception ex) {
mainArea.setText("Error ocurred" + ex.getMessage());
}
}
}
});
}
}
提前致謝
JEditorPane不是現代Web瀏覽器的基礎。 它支持渲染大多數HTML 3.2,僅此而已。 沒有JavaScript支持。 您應該查看有關Swing瀏覽器組件的現有問題,例如基於Java的Swing瀏覽器應支持JavaScript和Best Java / Swing瀏覽器組件 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.