1

我正在尝试将 JavaFx 浏览器组件添加到我的 Swing 应用程序中。我需要在选项卡式窗格中加载浏览器,并在运行时动态地将 url 设置为这些浏览器。所以我有以下代码将浏览器呈现到 JFrame 中,但输出不会以框架的大小出现。浏览器组件在选项卡式窗格内以小尺寸显示,而不是全帧尺寸。请指导我从以下代码中更正的位置:

    public class BrowserScene extends JPanel {

private static final long serialVersionUID = 5114913691608034106L;
private JFXPanel jfxPanel;
private WebEngine engine;

private JPanel panel = new JPanel(new BorderLayout());
private JLabel lblStatus = new JLabel();

private JButton btnGo = new JButton("Go");
private JTextField txtURL = new JTextField();
private JProgressBar progressBar = new JProgressBar();

private void initComponents() {
    jfxPanel = new JFXPanel();
    jfxPanel.setSize(new Dimension(1200, 1000));

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

    add(panel);     
}

private void createScene() {

    Platform.runLater(new Runnable() {
        @Override
        public void run() {

            WebView view = new WebView();
            Group group = new Group();
            Scene scene = new Scene(group);
            jfxPanel.setScene(scene);
            group.getChildren().add(view);

            engine = view.getEngine();
            engine.setJavaScriptEnabled(true);

            /*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() {
                                    frame.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>() {

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

public void loadURL(final StringBuffer buffer) {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            engine.loadContent(buffer.toString());
        }
    });
}

public String getUrl() {
    return engine.getLocation();
}

private String toURL(String str) {
    try {
        return new URL(str).toExternalForm();
    } catch (MalformedURLException exception) {
        return null;
    }
}

public BrowserScene() {
    initComponents();

    loadURL("http://www.google.co.in");
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JTabbedPane tabbedPane = new JTabbedPane();
            BrowserScene jfb = new BrowserScene();
            jfb.validate();  // update and redisplay
            tabbedPane.add(jfb);
            JFrame frame = new JFrame();
            frame.setLayout(new GridLayout());
            frame.add(tabbedPane);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension dim = tk.getScreenSize();
            frame.setMaximumSize(dim);
            frame.setMinimumSize(dim);
            frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
            frame.setMaximizedBounds(null);
            //  frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }
    });
}

}

4

1 回答 1

0

阅读..

http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm

这个简单的 Swing 浏览器

浏览器 java 已知 = Lobo Web Browser、Willow、xBrowser

于 2013-03-15T13:45:49.160 回答