美文网首页JavaSwing技术
Swing使用JavaFXweb组件

Swing使用JavaFXweb组件

作者: LinkedIn | 来源:发表于2018-01-18 12:37 被阅读0次

    概述

    swing中内嵌入web组件的 需要使用一些其他的jar包 ,但是如果使用javafx的组件,那么也比较的方便,性能也比较高.

    代码

    • webview 在javafx 中是作为 scene出现的所以不需要单独设置部件类型.

    • 下面是单独的地址处理方法

    private static void gotoURL(String url) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    webView.getEngine().load(url);
                }
            });
        }
    
    
    • swing嵌入fx 一般的写法
      这里注意 webview 最好是 静态化
        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);
                    }
                }
    
            });
    
        
        }
    
    }
    

    相关文章

      网友评论

        本文标题:Swing使用JavaFXweb组件

        本文链接:https://www.haomeiwen.com/subject/iuzioxtx.html