如何在我的应用中打开网址,而不是浏览器?

Ers*_*har 1 html java swing netbeans javafx

我想在我的应用中打开一个网址,而不是在浏览器中打开网址.我该怎么做?我想我需要一个webview.I使用netbeans桌面应用程序与jdk 6

如果需要javafx,我该如何使用它?请给一些教程?

Dav*_*amp 5

为什么不使用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