【问题标题】:How to open JavaFX browser in a separate thread如何在单独的线程中打开 JavaFX 浏览器
【发布时间】:2015-09-27 15:07:01
【问题描述】:

我有一个简单的JavaFx 浏览器,它通过一个JButton 启动,它被放置在一个JFrame 中。现在,我想将 JavaFx broser 放入一个单独的线程中,即使 JFrame 已关闭,该线程也应该运行。现在,如果我用那个 JButton 关闭 JFrame,JavaFx 浏览器也会关闭。我尝试了多种组合,通过互联网进行了研究,但没有成功。希望大家能帮我举个小例子。谢谢 !

 browser.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e) {
               if(e.getSource() == browser) {
                   
                
                   
                     
                     // create a JFXPanel, which will start the FX toolkit
                // if it's not already started: - folosit pt a integra componente de tip FX Stage in componente swing (mai vechi) (JFrame)
                JFXPanel fxPanel = new JFXPanel();
                
                 Platform.runLater(new Runnable() {
                     
                     
@Override
                    public void run() {
                       
                        Scene scene;
                        TextField addressField;
                        WebView webView;
                        WebEngine webEngine;
                        Stage stage = null;
                        Button reloadButton, goButton, backButton , forwardButton, historyList;
                      
                       
          
                        
                        HBox hBox = new HBox(5);
                        hBox.setAlignment(Pos.CENTER);
                        //The TextField for entering web addresses.
                        addressField = new TextField("Ionutz says: Enter the address here....");
                        addressField.setPrefColumnCount(50); //make the field at least 50 columns wide.
                        //Add all out navigation nodes to the vbox.
                        reloadButton = new Button("Reload page");
                        goButton = new Button("Search");
                        
                        
                        backButton = new Button("Back");
                        forwardButton = new Button("Forward");  
                        historyList = new Button("History");
                        
                        String urlSearch = "http://icons.iconarchive.com/icons/ampeross/qetto-2/24/search-icon.png";
                        String urlReload = "http://icons.iconarchive.com/icons/graphicloads/colorful-long-shadow/24/Arrow-reload-2-icon.png";
                        
                        String URLBack = "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/back-icon.png";
    String URLForward = "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/forward-icon.png";
    String URLHistory = "http://icons.iconarchive.com/icons/delacro/id/24/History-icon.png";
    
    goButton.setGraphic(new ImageView(urlSearch));
    
    reloadButton.setGraphic(new ImageView(urlReload));
        
    forwardButton.setGraphic(new ImageView(URLForward));
    
    backButton.setGraphic(new ImageView(URLBack));
    
    historyList.setGraphic(new ImageView(URLHistory));
                        
                        
                        hBox.getChildren().addAll(backButton, forwardButton, addressField, reloadButton, goButton, historyList);
                        //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.
                        fxPanel.setScene(scene);
                        JFrame browserFrame = new JFrame();
                        browserFrame.add(fxPanel);
                        browserFrame.setTitle("Ionutz Asaftei Browser");
                        browserFrame.setBounds(200, 200, 1280, 720);
                        browserFrame.setVisible(true);
                      
                        
                    
                          
                              // adaugam event handlers !!!! - cele mai simple pt Buttons
        reloadButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
              webEngine.reload();
              
            }
        });
        
        goButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
                webEngine.load("http://"+addressField.getText());
            }
        });
        
         
                         
        forwardButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent event) {
                            webEngine.getHistory().go(1); //avanseaza cu o pagina in functie de intrarile din history
                
                        }
                 });
        
        backButton.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
            public void handle(javafx.event.ActionEvent ev) {
               webEngine.getHistory().go(-1); //merge in urma cu o pagina in functie de intrarile din history
            }
        });
                }
                    
                 });
                   
                  
                           }
                         }
                        
                     });

【问题讨论】:

    标签: multithreading


    【解决方案1】:

    从用户应用程序的角度来看,JavaFX 工具包是单线程的。不可能产生多个线程来启动 WebView 实例。

    【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2019-04-12
    • 2013-03-11
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多