2015-09-27 41 views
0

我有一個簡單的JavaFx瀏覽器,它通過放入JFrame中的JButton啓動。現在,我想將JavaFx broser放入一個單獨的線程,即使JFrame已關閉,它也應該運行。現在,如果我關閉帶有JButton的JFrame,JavaFx瀏覽器也會關閉。我嘗試過多種組合,通過互聯網進行研究,但沒有成功。希望你們能以一個小例子來幫助我。謝謝 !如何在單獨的線程中打開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 
 
      } 
 
     }); 
 
       } 
 
        
 
       }); 
 
        
 
        
 
          } 
 
         } 
 
         
 
        });

回答

0

JavaFX的工具包是單線程從一個用戶應用點。生成多個線程啓動WebView實例是不可能的。

+0

爲什麼這個限制? :(如果這是唯一的方法,我將使瀏覽器與主應用程序分離。: –

+0

要理解UI工具箱中單線程規則的原因,請參閱Javafx:javafx之間的差異的一些評論。 concurent和Platform.runLater?](http://stackoverflow.com/questions/18710039/javafx-difference-between-javafx-concurent-and-platform-runlater)。 – jewelsea

+0

謝謝,很高興知道!:) –

相關問題