2014-02-22 20 views
0

好吧,大家好,我仍然在学习Java,只是搞了一些东西,我做了一个GUI,并有一个JEditorPane按下“Go”按钮时显示一个网页。网页将无法使用JEdior窗格

代码将无法工作:

private void goActionPerformed(java.awt.event.ActionEvent evt) {         
      String URL = url.getText(); 
    JEditorPane.setEditable(false); 

    try { 
     JEditorPane.setPage("www.google.com"); 
    }catch (IOException e) { 
     JEditorPane.setContentType("text/html"); 
     JEditorPane.setText("<html>Could not load " + URL); 
    } 
} 

Anyhelp是值得欢迎的感谢!

+0

“url”是一个URL对象吗?如果是这样,也许你需要使用'toString()'?你有没有显示任何错误?你可以在这里发布吗? –

+0

它的JTextField,如果多数民众赞成你的意思(新) – Fusion

+0

我更新的代码留在主窗口,甚至已经放置的URL,它将无法正常工作。 – Fusion

回答

0

尝试这样:

JFrame frame = new JFrame(); 
    JTextField field = new JTextField(); 
    frame.add(field); 
    frame.pack(); 
    frame.setVisible(true); 

    JEditorPane pane = new JEditorPane(); 
    try { 
     pane.setPage(field.getText()); 
     ... 
    } 
    catch (IOException e) { 
     pane.setContentType("text/html"); 
     pane.setText("<html>Could not load "); 
    } 
... 
1

setPage需要一个有效的协议前缀

jEditorPane.setPage("http://www.google.com"); 

确保您的文本框也有一个这样的前缀(或者至少是URL链接参数以及形成的)

+0

这让我走得更远,但它看起来像http://gyazo.com/c5b20b7a3e641e4e5a1584673eaee518 – Fusion

+0

是的,不幸的是'JEditorPane'只支持HTML 3.2。您可以嵌入JavaFX的WebView。这里是Oracle的[示例](http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm) – Reimeus

+0

有没有办法通过Swing添加webview?因为这是我的选择。 http://gyazo.com/3af0d160ca7517e73ab52e497ef6dea0 – Fusion