如何在JFrame中打开网页网址?

时间:2014-09-23 11:29:28

标签: java swing jframe

所以我有这个代码创建了一个JFrame:

/* FrameDemo.java requires no other files. */
public class bot {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel emptyLabel = new JLabel("");
        emptyLabel.setPreferredSize(new Dimension(400, 400));
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

             }
        });
     }
 }

我有以下用于打开网页网址的代码:

public class fun {
    public static void main(String[] a) {
        try {
            URI uri = new URI("www.google.lt");
            Desktop desktop = null;
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();
            }
            if (desktop != null)
                desktop.browse(uri);
        } catch (IOException ioe) { 

            ioe.printStackTrace();
        } catch (URISyntaxException use) {
            use.printStackTrace();
        }
    }
}

如何加入这些代码?并使网页出现在JFrame中?也许有像php这样的命令包含?

1 个答案:

答案 0 :(得分:0)

使用javafx组件库的WebView组件(现在包含在Java中)并将其包装在JFXPanel中,以便您可以在Swing UI中使用它。

或者,尝试使用TeamDev中的商业jxBrowser组件。