天天看點

Swing使用JavaFXweb元件

概述

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);
        }
      }

    });

  
  }

}