2017-04-20 45 views
0

我正在嘗試創建此LayoutJavaFx多個佈局

我試着使用:

StackPane rootPane = new StackPane(); 
Scene scene = new Scene(rootPane,...); 
Pane pane1 = new Pane(); 
Pane pane2 = new Pane(); 
rootPane.getChildren().addAll(pane1,pane2); 

讓我創建一個菜單欄以及直接在其下面的文本字段,但它不會讓我的文本字段被通過菜單欄隱藏。

我不確定在我的情況下需要哪些。我看過vbox--這與我所需要的類似,但我不確定如何在最後一行中添加2個表格,並且有間隙

如果你能指點我的方向需要。

回答

2

StackPane在這裏不是一個好的選擇:它只是將子節點按z順序堆疊在一起。我建議您閱讀tutorial on layouts以獲取所有內置佈局窗格的完整說明,但一種選擇是使用VBox。要將物品放置在最下面一排,您可以使用一個AnchorPane,其中一個項目固定在左側,另一個固定在右側。

下面是使用這個方法的SSCCE:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.Menu; 
import javafx.scene.control.MenuBar; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class LayoutExample extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     VBox root = new VBox(5); 
     root.setPadding(new Insets(5)); 

     MenuBar menuBar = new MenuBar(); 
     menuBar.getMenus().add(new Menu("File")); 

     TextArea textArea = new TextArea(); 
     VBox.setVgrow(textArea, Priority.ALWAYS); 

     AnchorPane bottomRow = new AnchorPane(); 
     Label table1 = new Label("Table 1"); 
     table1.setStyle("-fx-background-color: gray"); 
     table1.setMinSize(200, 200); 
     Label table2 = new Label("Table 2"); 
     table2.setStyle("-fx-background-color: gray"); 
     table2.setMinSize(200, 200); 

     AnchorPane.setLeftAnchor(table1, 0.0); 
     AnchorPane.setRightAnchor(table2, 0.0); 
     bottomRow.getChildren().addAll(table1, table2); 

     root.getChildren().addAll(menuBar, textArea, bottomRow); 

     Scene scene = new Scene(root, 800, 800); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

enter image description here

另一個類似的,方法是使用一個BorderPane爲根,在頂部的菜單欄,在文本區中心和底部的錨窗格。