如何获取按钮来填充javafx网格窗格? [英] How do I get buttons to fill a javafx gridpane?

查看:184
本文介绍了如何获取按钮来填充javafx网格窗格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Swing具有GridLayout,它允许您为小部件数组(例如3X4)指定大小.然后,小部件将填充它们占据的面板.您如何在JavaFX中获得类似的效果?

Java Swing has GridLayout, which allows you to specify a size for an array of widgets such as 3X4. The widgets then fill the panel they occupy. How do you get a similar effect in JavaFX?

推荐答案

我想您是在问如何让节点填充网格窗格中为其单元分配的空间.

I think you are asking how you get a node to fill the space allocated to its cell in a grid pane.

有两种方法可以做到这一点.您可以使用静态GridPane方法将特定于GridPane的属性设置应用于节点:

There are a couple of ways to do this. You can either use the static GridPane methods to apply GridPane-specific property settings to the nodes:

GridPane.setFillWidth(myButton, true);
GridPane.setFillHeight(myButton, true);

另一种方法是在网格窗格本身上指定列约束和行约束:

The other way is to specify column constraints and row constraints on the grid pane itself:

GridPane grid = new GridPane();
for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
    RowConstraints rc = new RowConstraints();
    rc.setVgrow(Priority.ALWAYS) ; // allow row to grow
    rc.setFillHeight(true); // ask nodes to fill height for row
    // other settings as needed...
    grid.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < numColumns; colIndex++) {
    ColumnConstraints cc = new ColumnConstraints();
    cc.setHgrow(Priority.ALWAYS) ; // allow column to grow
    cc.setFillWidth(true); // ask nodes to fill space for column
    // other settings as needed...
    grid.getColumnConstraints().add(cc);
}

在任何一种情况下,都需要让节点增长,以便它们尊重网格窗格发出的增长请求.因此,例如:

In either case, you need to let the nodes grow in order for them to respect the request to grow that the grid pane makes. So, e.g.:

Button myButton = new Button("Click");
myButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
grid.add(myButton, 0, 0);

SSCCE(使用行和列约束方法):

SSCCE (using row and column constraints method):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

public class KeyPad extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = new GridPane();
        int numRows = 4 ;
        int numColumns = 3 ;
        for (int row = 0 ; row < numRows ; row++ ){
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            grid.getRowConstraints().add(rc);
        }
        for (int col = 0 ; col < numColumns; col++ ) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            grid.getColumnConstraints().add(cc);
        }

        for (int i = 0 ; i < 9 ; i++) {
            Button button = createButton(Integer.toString(i+1));
            grid.add(button, i % 3, i / 3);
        }
        grid.add(createButton("#"), 0, 3);
        grid.add(createButton("0"), 1, 3);
        grid.add(createButton("*"), 2, 3);

        Scene scene = new Scene(grid);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Button createButton(String text) {
        Button button = new Button(text);
        button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        button.setOnAction(e -> System.out.println(text));
        return button ;
    }

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

这篇关于如何获取按钮来填充javafx网格窗格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆