为什么不使用JEditorPane,setContentType()和setText().
您可以设置内容类型,然后从URL repsonse获取HTML并设置JEditorPane文本:
editor.setContentType( "text/html" );
editor.setText( "<html><body>Hello, world</body></html>" );
Run Code Online (Sandbox Code Playgroud)
更新:
虽然有一些小问题,但这是一个小例子:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JEditorPaneTest extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();
JEditorPane editor = new JEditorPane();
frame.getContentPane().add(editor);
editor.setContentType("text/html");
URL url = null;
try {
url = new URL("http://www.google.co.za");
} catch (MalformedURLException ex) {
Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
} catch (IOException ex) {
Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
StringBuffer response = new StringBuffer();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine).append("\n");
}
in.close();
} catch (IOException ex) {
Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
}
// editor.setText("<html><body>Hello, world</body></html>");
editor.setText(response.toString());
editor.setEditable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
你应该考虑使用JavaFX,尽管这有WebView你需要的:http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm
在此处下载:http://www.oracle.com/technetwork/java/javafx/downloads/index.html
要设置Java FX和netbeans,请访问:http://netbeans.org/kb/docs/java/javafx-setup.html