setFillWidth 在 JavaFX 中不起作用



我有这个代码:

public class Minesweeper extends Application {
private MenuBar menuBar;
private Stage primaryStage;
private final VBox vbox = new VBox(8);
private GridPane gridpane;
@Override
public void start(Stage primaryStage) {
    this.primaryStage=primaryStage;
    /*createMenu();
    //ImageViewPane viewPane = new ImageViewPane(gridpane);
    vbox.getChildren().addAll(menuBar,gridpane);
    vbox.setBackground(new Background(fills));*/
    HBox hbox = new HBox(50);
    hbox.setAlignment(Pos.CENTER); // default TOP_LEFT
    VBox vbox1 = new VBox();
    //vbox1.setAlignment(Pos.BOTTOM_CENTER);
    vbox1.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");
    vbox1.setPrefSize(300, 300);
    vbox1.setFillWidth(true);
    VBox vbox2 = new VBox(10);
    vbox2.setAlignment(Pos.CENTER);
    vbox2.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");
    VBox vbox3 = new VBox(20);
    vbox3.setAlignment(Pos.TOP_CENTER);
    vbox3.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");
    for (int i = 0; i < 5; i++)
    {
        Button bt = new Button("Button " + (i+1));
        Button bt2 = new Button("Button " + (i+1)); // unfortunately there´s no "clone" or "copy" method
        Button bt3 = new Button("Button " + (i+1));
        vbox1.getChildren().add(bt);
        vbox2.getChildren().add(bt2);
        vbox3.getChildren().add(bt3);
    }
    hbox.getChildren().addAll(vbox1, vbox2, vbox3);
    Scene scene = new Scene(hbox, 350, 250); // the hbox is the root node
    //Scene scene = new Scene(vbox,600,600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Minesweeper");
    primaryStage.show();
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

我希望vbox1中的按钮填充剩余的空间(setfillwidth),但它不起作用。也许我没有正确使用它。

有人能向我解释一下吗?

感谢

尝试将按钮的maxWidth属性设置为无穷大,以允许它占用所有可用空间。

bt.setMaxWidth(Double.POSITIVE_INFINITY);

有关详细信息,请参阅此答案。

相关内容

  • 没有找到相关文章

最新更新