这可能吗?我做了一个简单的网络浏览器,它可以很好地查看一些页面,但大多数都搞砸了,我相信这可能是因为这个(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不是现代网络浏览器的基础。它支持呈现大多数 HTML 3.2,仅此而已。没有 JavaScript 支持。您应该查看有关 Swing 浏览器组件的现有问题,例如 Java based Swing Browser should support JavaScript和 Best Java/Swing browser component .
关于在 Java 网络浏览器中工作的 Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200171/