A SimpleSwingBrowser.java
有关描述,请参见将JavaFX集成到Swing应用程序中。
法律条款和版权声明
/* * 版权所有 (c) 2011, 2014, Oracle及其附属公司。 * 保留所有权利。使用受许可条款约束。 * * 此文件可在以下许可下使用和许可: * * 源代码的重新发布和使用,无论是否进行修改,只要满足以下条件: * * - 必须保留上述版权声明、此条件列表和以下免责声明。 * - 以二进制形式重新发布的代码必须在文档和/或其他提供的材料中复制上述版权声明、此条件列表和以下免责声明。 * - 未经特定事先书面许可,不得使用Oracle或其贡献者的名称来认可或推广从本软件派生的产品。 * * 本软件由版权持有人和贡献者“按原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性和适用性的暗示保证。 * 在任何情况下,版权所有人或贡献者均不对任何直接、间接、偶然、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购、使用、数据或利润损失,或业务中断)承担任何责任,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他)。 */
代码
package simpleswingbrowser; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.embed.swing.JFXPanel; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.MalformedURLException; import java.net.URL; import static javafx.concurrent.Worker.State.FAILED; public class SimpleSwingBrowser extends JFrame { private final JFXPanel jfxPanel = new JFXPanel(); private WebEngine engine; private final JPanel panel = new JPanel(new BorderLayout()); private final JLabel lblStatus = new JLabel(); private final JButton btnGo = new JButton("Go"); private final JTextField txtURL = new JTextField(); private final JProgressBar progressBar = new JProgressBar(); public SimpleSwingBrowser() { super(); initComponents(); } private void initComponents() { createScene(); ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { loadURL(txtURL.getText()); } }; btnGo.addActionListener(al); txtURL.addActionListener(al); progressBar.setPreferredSize(new Dimension(150, 18)); progressBar.setStringPainted(true); JPanel topBar = new JPanel(new BorderLayout(5, 0)); topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5)); topBar.add(txtURL, BorderLayout.CENTER); topBar.add(btnGo, BorderLayout.EAST); JPanel statusBar = new JPanel(new BorderLayout(5, 0)); statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5)); statusBar.add(lblStatus, BorderLayout.CENTER); statusBar.add(progressBar, BorderLayout.EAST); panel.add(topBar, BorderLayout.NORTH); panel.add(jfxPanel, BorderLayout.CENTER); panel.add(statusBar, BorderLayout.SOUTH); getContentPane().add(panel); setPreferredSize(new Dimension(1024, 600)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } private void createScene() { Platform.runLater(new Runnable() { @Override public void run() { WebView view = new WebView(); engine = view.getEngine(); engine.titleProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { SimpleSwingBrowser.this.setTitle(newValue); } }); } }); engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() { @Override public void handle(final WebEvent<String> event) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { lblStatus.setText(event.getData()); } }); } }); engine.locationProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { txtURL.setText(newValue); } }); } }); engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() { @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { progressBar.setValue(newValue.intValue()); } }); } }); engine.getLoadWorker() .exceptionProperty() .addListener(new ChangeListener<Throwable>() { @Override public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) { if (engine.getLoadWorker().getState() == FAILED) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JOptionPane.showMessageDialog( panel, (value != null) ? engine.getLocation() + "\n" + value.getMessage() : engine.getLocation() + "\nUnexpected error.", "Loading error...", JOptionPane.ERROR_MESSAGE); } }); } } }); jfxPanel.setScene(new Scene(view)); } }); } public void loadURL(final String url) { Platform.runLater(new Runnable() { @Override public void run() { String tmp = toURL(url); if (tmp == null) { tmp = toURL("http://" + url); } engine.load(tmp); } }); } private static String toURL(String str) { try { return new URL(str).toExternalForm(); } catch (MalformedURLException exception) { return null; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { SimpleSwingBrowser browser = new SimpleSwingBrowser(); browser.setVisible(true); browser.loadURL("http://oracle.com"); } }); } }