2015-10-05 41 views
0

我有以下簡單的代碼,其中有一個水平方塊,其中有兩個按鈕,其餘部分爲空。現在,我想在用戶按下「當前」按鈕後在免費部分查看Webview。我已經做了類似的事情,但我無法管理佈局,因爲它是Jframe + JavaFX。我想在JavaFx中完全重新構建,因此我想將webview放入jfxPanel(我認爲這是最好的解決方案,其他建議真的很感激)。當我按下按鈕時,我應該看到webview,在這種情況下是當前的,因此我創建了一個句柄。那麼,我該怎麼做?從JavaFX中的按鈕創建一個webview

在此先感謝

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.geometry.VPos; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Hyperlink; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.TilePane; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.CycleMethod; 
import javafx.scene.paint.LinearGradient; 
import javafx.scene.paint.Stop; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class Launcher extends Application { 


     public static void main(String[] args) { 
     launch(Launcher.class, args); 
    } 
     Button buttonCurrent; 

    static HBox web; 
    static MyBrowser browser_lau; 


    public void start(Stage stage) { 



     BorderPane border = new BorderPane(); 

     HBox hbox = addHBox(); 


     border.setTop(hbox); 

     Scene scene = new Scene(border); 
     stage.setScene(scene); 
     stage.setTitle("Layout Sample"); 
     stage.show(); 

    buttonCurrent.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event) { 
       System.out.println("Hello World!"); 
       final JFXPanel jfxPanel = new JFXPanel(); 

       //border.setStyle("-fx-background-color: #ff0000;"); 




      } 
     }); 


    } 




    public HBox addHBox() { 

     HBox hbox = new HBox(); 
     hbox.setPadding(new Insets(15, 12, 15, 12)); 
     hbox.setSpacing(10); // Gap between nodes 
     hbox.setStyle("-fx-background-color: #336699;"); 

     buttonCurrent = new Button("Current"); 
     buttonCurrent.setPrefSize(100, 20); 

     Button buttonProjected = new Button("Projected"); 
     buttonProjected.setPrefSize(100, 20); 

     hbox.getChildren().addAll(buttonCurrent, buttonProjected); 

     return hbox; 
    } 

}  



} 
+0

如果你打算只寫使用JavaFX技術(根據您的問題描述,似乎最好)的UI,你不需要[JFXPanel(https://開頭的文檔。 oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html) - 這就是混合JavaFX和Swing技術。 – jewelsea

+0

是的,我想用JavaFX @jewelsea來做UI。但我不知道如何繼續。 – user2556079

+0

[教程](http://docs.oracle.com/javase/8/javafx/embedded-browser-tutorial/overview.htm#JFXWV135)介紹瞭如何在網頁視圖中加載網頁的一些基本示例。 –

回答

0

下面是一些示例代碼,讓你開始,它創造了幾個網頁視圖和它們之間切換,這取決於一個單選按鈕選擇。

current population

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

public class CountThePeople extends Application { 
    private static final String CURRENT_URL = 
     "http://www.census.gov/popclock/"; 
    private static final String PROJECTED_URL = 
     "http://www.sciencedaily.com/releases/2015/08/150810110634.htm"; 

    @Override 
    public void start(final Stage stage) throws Exception { 
     WebView current = new WebView(); 
     current.getEngine().load(CURRENT_URL); 
     WebView projected = new WebView(); 
     projected.getEngine().load(PROJECTED_URL); 

     StackPane viewHolder = new StackPane(); 

     ToggleGroup choice = new ToggleGroup(); 

     RadioButton currentRadio = new RadioButton("Current"); 
     currentRadio.setToggleGroup(choice); 
     RadioButton projectedRadio = new RadioButton("Projected"); 
     projectedRadio.setToggleGroup(choice); 

     choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> { 
      if (projectedRadio == newValue) { 
       viewHolder.getChildren().setAll(projected); 
      } else { 
       viewHolder.getChildren().setAll(current); 
      } 
     }); 

     choice.selectToggle(currentRadio); 

     VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder); 
     layout.setPadding(new Insets(10)); 

     stage.setTitle("World Population"); 
     stage.setScene(new Scene(layout)); 
     stage.show(); 
    } 

    public static void main(String[] args) throws Exception { 
     launch(args); 
    } 
} 
相關問題