2017-10-10 65 views
0

因此,我試圖讓文本在左邊,按鈕在右邊,文本應該具有不變的大小,並且按鈕應該調整大小以填充窗口的其餘部分。將舞臺劃分爲2個網格JavaFX

這是到目前爲止我的結果:

so far...

我不希望我的文字了按鈕,我希望他們能夠分享整個窗口。

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 


public class Main extends Application { 

    GridPane buttons = new GridPane(); 
    GridPane textGrid = new GridPane(); 
    @Override 
    public void start(Stage primaryStage) { 

     StackPane root = new StackPane(); 
     Button button1 = new Button(); 
     Button button2 = new Button(); 
     Button button3 = new Button(); 
     Button button4 = new Button(); 
     Button button5 = new Button(); 

     button1.setText("Button1"); 
     button2.setText("Button4"); 
     button3.setText("Button3"); 
     button4.setText("Button4"); 
     button5.setText("Button5"); 


     TextArea text1 = new TextArea(); 
     text1.setText("Test"); 
     text1.setPrefSize(100, 100); 

     button1.prefWidthProperty().bind(buttons.widthProperty()); 
     button2.prefWidthProperty().bind(buttons.widthProperty()); 
     button3.prefWidthProperty().bind(buttons.widthProperty()); 
     button4.prefWidthProperty().bind(buttons.widthProperty()); 
     button5.prefWidthProperty().bind(buttons.widthProperty()); 

     button1.prefHeightProperty().bind(buttons.heightProperty()); 
     button2.prefHeightProperty().bind(buttons.heightProperty()); 
     button3.prefHeightProperty().bind(buttons.heightProperty()); 
     button4.prefHeightProperty().bind(buttons.heightProperty()); 
     button5.prefHeightProperty().bind(buttons.heightProperty()); 


     buttons.addColumn(0, button1, button2, button3, button4, button5); 

     textGrid.addColumn(0, text1); 


     Scene scene = new Scene(root, 280, 180); 

     root.getChildren().addAll(buttons, textGrid); 

     buttons.setAlignment(Pos.TOP_RIGHT); 
     textGrid.setAlignment(Pos.TOP_LEFT); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

} 
+0

使用'HBox'而不是'StackPane'?還是一個'BorderPane',具體取決於你希望他們在調整大小時的表現?請參閱http://docs.oracle.com/javase/8/javafx/layout-tutorial/builtin_layouts.htm#JFXLY102 –

+0

感謝您的建議,可能會使用HBox – murilo

回答

4

通常最好是讓佈局窗格處理佈局管理,而不是試圖通過綁定來管理佈局。

這裏有一個例子:

layout

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

import java.util.stream.IntStream; 

public class Main extends Application { 

    private static final int N_BUTTONS = 5; 

    @Override 
    public void start(Stage stage) { 
     VBox buttonLayout = new VBox(
       10, 
       IntStream.range(0, N_BUTTONS) 
         .mapToObj(this::createButton) 
         .toArray(Button[]::new) 
     ); 
     HBox.setHgrow(buttonLayout, Priority.ALWAYS); 

     TextArea textArea = new TextArea("Test"); 
     textArea.setPrefWidth(100); 
     textArea.setMaxWidth(TextArea.USE_PREF_SIZE); 
     textArea.setMinWidth(TextArea.USE_PREF_SIZE); 

     HBox layout = new HBox(10, textArea, buttonLayout); 
     layout.setPadding(new Insets(10)); 

     Scene scene = new Scene(layout); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    private Button createButton(int i) { 
     Button button = new Button("Button " + i); 
//  button.setMaxWidth(Double.MAX_VALUE); 
     button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
     VBox.setVgrow(button, Priority.ALWAYS); 

     return button; 
    } 

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

這裏有一對夫婦的事情,我想指出,基於樣本:

  1. 由於這些按鈕是如此的相似,創建循環中的按鈕而不是代碼中的按鈕。我在地圖和toArray上使用了IntStream範圍,但您可以使用循環標準(可能更容易理解)做同樣的事情。
  2. 使用標準佈局窗格的組合來實現您的佈局。例如,按鈕是垂直間隔的,所以將它們放在一個VBox中,文本和按鈕相互水平,所以使用HBox。
  3. 在佈局上使用約束來將它們按摩到執行您喜歡的佈局中,例如,HBox.setHgrow(buttonLayout, Priority.ALWAYS);告訴Box始終將任何額外的額外空間分配給buttonLayout,以便按鈕將填充任何其餘區域。
  4. 在單個節點上設置約束以調整它們的大小,例如下面的代碼爲textArea創建了一個固定的寬度,它不會發生變化(如果您願意,您可以使用類似的代碼來建立固定的高度):

    textArea.setPrefWidth(100); 
    textArea.setMaxWidth(TextArea.USE_PREF_SIZE); 
    textArea.setMinWidth(TextArea.USE_PREF_SIZE); 
    
  5. 某些控件會自動擴展自己,超越自己的最大大小,按鈕不被默認情況下,啓用此行爲,請使用以下代碼(如果你只是想的寬度擴大,而不是高度,那麼你只會設置最大寬度而不是最大尺寸):

    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
    
  6. 而不是像本示例中那樣定義代碼中的佈局,而是使用諸如SceneBuilder之類的工具以可視方式創建場景並將佈局保存爲FXML file,以便將佈局與代碼分開(類似地將任何樣式放置在外部CSS文件)。