Swing使用JavaFXweb組件

概述

swing中內(nèi)嵌入web組件的 需要使用一些其他的jar包 ,但是如果使用javafx的組件,那么也比較的方便,性能也比較高.

代碼

  • webview 在javafx 中是作為 scene出現(xiàn)的所以不需要單獨設(shè)置部件類型.

  • 下面是單獨的地址處理方法

private static void gotoURL(String url) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                webView.getEngine().load(url);
            }
        });
    }

  • swing嵌入fx 一般的寫法
    這里注意 webview 最好是 靜態(tài)化
    Platform.runLater(new Runnable() {
            @Override
            public void run() {
                webView = new WebView();
                jFXPanel.setScene(new Scene(webView));
                webView.getEngine().load("http://www.baidu.com");
            }
        });
  • 剩下的就是布局處理 你喜歡就好 , 這里我選擇的了一個splash,出場動畫, 可要可不要.
public class SwingFinal {

    static WebView webView = null;

    private static void gotoURL(String url) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                webView.getEngine().load(url);
            }
        });
    }

    /**
     * @param args
     *            the command line arguments
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws MalformedURLException, URISyntaxException {
        // TODO code application logic here
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        JFXPanel jFXPanel = new JFXPanel();
        frame.add(jFXPanel, "Center");

        JPanel controlPanel = new JPanel();
        frame.add(controlPanel, "North");
        JTextField urlField = new JTextField();
        JButton goButton = new JButton("GO");
        ///////////////////////////////////////////////////////////////////////////////////
        urlField.setText("http://www.baidu.com");

        controlPanel.setLayout(new BorderLayout());
        urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
        controlPanel.add(urlField, BorderLayout.WEST);
        controlPanel.add(goButton, BorderLayout.EAST);

        controlPanel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                controlPanel.setLayout(new BorderLayout());
                urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
                controlPanel.add(urlField, BorderLayout.WEST);
                controlPanel.add(goButton, BorderLayout.EAST);
            }

        });
        frame.addWindowStateListener(new WindowStateListener() {
            @Override
            public void windowStateChanged(WindowEvent e) {
                controlPanel.setLayout(new BorderLayout());
                urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
                controlPanel.add(urlField, BorderLayout.WEST);
                controlPanel.add(goButton, BorderLayout.EAST);
            }
        });
        goButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String urlString = urlField.getText();
                gotoURL(urlString);
            }

        });
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                webView = new WebView();
                jFXPanel.setScene(new Scene(webView));
                webView.getEngine().load("http://www.baidu.com");
            }
        });

        JWindow splashWindow = new JWindow();
        splashWindow.setSize(1024, 768);
        splashWindow.setLocationRelativeTo(null);
        splashWindow.setLayout(new BorderLayout());
        File file = new File(SwingFinal.class.getResource("fox.png").toURI());
        ImageIcon icon = new ImageIcon(file.toURL());
        JLabel label = new JLabel(icon);
        splashWindow.add(label);
        Thread t = new Thread() {
            public void run() {
                frame.setVisible(false);
                splashWindow.setVisible(true);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(SwingFinal.class.getName()).log(Level.SEVERE, null, ex);
                }
                splashWindow.setVisible(false);
                frame.setVisible(true);

            }
        };
        t.setDaemon(true);
        t.start();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(null, "????", "????", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }

        });

    
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容