【问题标题】:Make Objects of Gridpane fill screen使 Gridpane 的对象填充屏幕
【发布时间】:2015-12-07 06:34:50
【问题描述】:

我目前正在尝试学习 JavaFX。我已经创建了一个带有网格窗格的小型设置,如下面的屏幕截图所示

我将网格窗格的背景设置为红色,以便您可以看到它填满了整个屏幕。但不幸的是,标签和文本字段的大小保持不变。我希望它们随着网格窗格的大小而缩放。

下面是目前的代码:

   @Override
public void start(Stage primaryStage) throws Exception{
    primaryStage.setTitle("Test Application");

    grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    grid.setMinSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    textWelcome = new Text("Welcome");
    textWelcome.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(textWelcome, 0, 0, 2, 1);

    labelName = new Label("User Name: ");
    grid.add(labelName, 0,1);

    nameField = new TextField();
    grid.add(nameField, 1,1);

    labelPW = new Label("Password: ");
    grid.add(labelPW,0,2);

    pwField = new PasswordField();
    grid.add(pwField,1,2);

    buttonSign = new Button("Sign in");
    HBox hBox = new HBox(10);
    hBox.setAlignment(Pos.BOTTOM_RIGHT);
    hBox.getChildren().add(buttonSign);
    grid.add(hBox,1,3);

    grid.setStyle("-fx-background-color:red");
    //grid.setGridLinesVisible(true);
    scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT);
    primaryStage.setScene(scene);
    primaryStage.show();
}

所以现在我想知道如何让整个事情放大,这样两边就没有很大的余量。

【问题讨论】:

    标签: java javafx gridpane


    【解决方案1】:

    您可以使用ColumnConstraintsRowConstraints 来配置列和行的行为。您还可以使用静态 GridPane 方法在单个节点上设置属性。

    请参阅documentation 中标题为“行/列大小”和“可选布局约束”的部分

    这是一个简单的例子:

    import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.ColumnConstraints;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    public class GridPaneColumnGrowExample extends Application {
    
        private static final double WINDOW_WIDTH = 600 ;
        private static final double WINDOW_HEIGHT = 400 ;
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            primaryStage.setTitle("Test Application");
    
            GridPane grid = new GridPane();
            grid.setAlignment(Pos.CENTER);
            grid.setHgap(10);
            grid.setVgap(10);
            grid.setPadding(new Insets(10, 10, 10, 10));
            grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            grid.setMinSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
            ColumnConstraints leftCol = new ColumnConstraints();
            leftCol.setHalignment(HPos.RIGHT);
            leftCol.setHgrow(Priority.NEVER);
    
            ColumnConstraints rightCol = new ColumnConstraints();
            rightCol.setHgrow(Priority.ALWAYS);
    
            grid.getColumnConstraints().addAll(leftCol, rightCol);
    
            Text textWelcome = new Text("Welcome");
            textWelcome.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
            grid.add(textWelcome, 0, 0, 2, 1);
    
            // override column constraints alignment for this cell
            GridPane.setHalignment(textWelcome, HPos.LEFT);
    
            Label labelName = new Label("User Name: ");
            grid.add(labelName, 0,1);
    
            TextField nameField = new TextField();
            grid.add(nameField, 1,1);
    
            Label labelPW = new Label("Password: ");
            grid.add(labelPW,0,2);
    
            PasswordField pwField = new PasswordField();
            grid.add(pwField,1,2);
    
            Button buttonSign = new Button("Sign in");
            grid.add(buttonSign,1,3);
            GridPane.setHalignment(buttonSign, HPos.RIGHT);
    
            grid.setStyle("-fx-background-color:red");
            //grid.setGridLinesVisible(true);
            Scene scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我现在只是求助于使用场景生成器。它肯定比在代码中这样做更令人沮丧
    • 您可以直接在 Scene Builder 中设置所有这些属性。你如何编写代码并没有什么不同,它仍然必须以某种方式编写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2017-06-11
    • 1970-01-01
    • 2012-03-31
    相关资源
    最近更新 更多