需要使JavaFX嵌入式浏览器在动作侦听器中工作

时间:2015-09-19 20:25:21

标签: javafx

当我按下Jbutton时,我需要打开JavaFX窗口。我尝试过各种各样的方式,我在互联网上搜索过,但我什么都没发现。库已正确设置,但无法正常工作。

怎样才能使这个工作?非常感谢 !

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import javax.swing.*;
import java.awt.event.ActionListener;


public class UTF8  {

    public static void main(String[] args)   {
        UTF8 object = new UTF8();
        object.UTF8();
    }

    public void UTF8() {
        JButton browser = new JButton();
        JFrame frame = new JFrame();
        frame.add(browser); frame.setSize(100,100); frame.setVisible(true);
        browser.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(e.getSource() == browser) {

                    Scene scene;
                    TextField addressField;
                    WebView webView;
                    WebEngine webEngine;
                    Stage stage = null;
                    HBox hBox = new HBox(5);
                    hBox.setAlignment(Pos.CENTER);

                    //The TextField for entering web addresses.
                    addressField = new TextField("Enter Web address here...");
                    addressField.setPrefColumnCount(50); //make the field at least 50 columns wide.
                    //Add all out navigation nodes to the vbox.
                    hBox.getChildren().addAll(addressField);
                    //Our weiv that display ther page.
                    webView = new WebView();

                    //the engine that manages our pages.
                    webEngine = webView.getEngine();
                    webEngine.setJavaScriptEnabled(true);
                    webEngine.load("http://www.google.ro");
                    //our main app layout with 5 regions.
                    BorderPane root = new BorderPane();
                    root.setPrefSize(1280, 720);
                    //Add every node to the BorderPane.
                    root.setTop(hBox);
                    root.setCenter(webView);
                    //Our scene is where all the action in JavaFX happens.  A scene holds all Nodes, and its root node is our BorderPane.
                    scene = new Scene(root);
                    //the stage manages the scene.
                    stage.setTitle("Ionutz Asaftei Browser");
                    stage.setScene(scene);
                    stage.show();
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

如果你还没有,你应该通过Integrating JavaFX into Swing Applications

在您的情况下,您需要使用JFXPanel作为中间节点,它接受JavaFX场景作为其内容并扩展JComponent,这可以帮助您将其设置为JFrame作为组件。

将所有JavaFX内容设置为场景的根目录,然后使用jfxpanel.setScene()将场景添加到此JFXPanel。 您需要在JavaFX应用程序线程上执行此操作,即将其包装在Platform.runLater()

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import javax.swing.*;


public class UTF8  {

    public static void main(String[] args)   {
        UTF8 object = new UTF8();
        object.UTF8();
    }

    public void UTF8() {

        JButton browser = new JButton("Click me");

        JFrame frame = new JFrame();
        frame.add(browser);
        frame.setSize(100,100);
        frame.setVisible(true);

        browser.addActionListener(e -> {
            if (e.getSource() == browser) {
                JFXPanel jfxPanel = new JFXPanel();
                Platform.runLater(() -> {
                    initFX(jfxPanel);
                });
                JFrame newFrame = new JFrame();
                newFrame.setSize(300, 200);
                newFrame.setContentPane(jfxPanel);
                newFrame.setVisible(true);
            }
        });
    }

    private static void initFX(JFXPanel fxPanel) {
        // This method is invoked on the JavaFX thread
        TextField addressField;
        WebView webView;
        WebEngine webEngine;
        HBox hBox = new HBox(5);
        hBox.setAlignment(Pos.CENTER);

        //The TextField for entering web addresses.
        addressField = new TextField("Enter Web address here...");
        addressField.setPrefColumnCount(50); //make the field at least 50 columns wide.
        //Add all out navigation nodes to the vbox.
        hBox.getChildren().addAll(addressField);
        //Our weiv that display ther page.
        webView = new WebView();

        //the engine that manages our pages.
        webEngine = webView.getEngine();
        webEngine.setJavaScriptEnabled(true);
        webEngine.load("http://www.google.ro");
        //our main app layout with 5 regions.
        BorderPane root = new BorderPane();
        root.setPrefSize(1280, 720);
        //Add every node to the BorderPane.
        root.setTop(hBox);
        root.setCenter(webView);
        //Our scene is where all the action in JavaFX happens.  A scene holds all Nodes, and its root node is our BorderPane.
        Scene scene = new Scene(root);
        //the stage manages the scene.
        fxPanel.setScene(scene);
    }
}