【发布时间】:2016-04-29 00:16:30
【问题描述】:
我正在尝试处理一个 VBox,我想用最多 5 个包含最多 5 个按钮的 HBox 填充该 VBox。 我还需要这些按钮可以调整大小,但我无法完成。 已经尝试使用返回 HBox 的静态方法,从
调用它primaryStage.getChildren.addAll(hBoxMethod());
非常感谢您的帮助:)
【问题讨论】:
-
请告诉我们您尝试了什么
我正在尝试处理一个 VBox,我想用最多 5 个包含最多 5 个按钮的 HBox 填充该 VBox。 我还需要这些按钮可以调整大小,但我无法完成。 已经尝试使用返回 HBox 的静态方法,从
调用它primaryStage.getChildren.addAll(hBoxMethod());
非常感谢您的帮助:)
【问题讨论】:
不完全确定您尝试做什么。但是,如果您尝试使用 5x5 自动调整大小的按钮(如标题所示)填充您的屏幕:为什么不使用 GridPane 和 AnchorPanes 的组合来调整大小。
package sample;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
AnchorPane rootPane = new AnchorPane();
Scene scene = new Scene(rootPane, 400, 300);
GridPane grid = new GridPane();
MaximizeInAnchorPane(grid);
// make column and rows resize
for (int i=0;i <5;i++)
{
ColumnConstraints cConstrain = new ColumnConstraints();
cConstrain.setHgrow(Priority.SOMETIMES);
grid.getColumnConstraints().add(cConstrain);
RowConstraints rConstrain = new RowConstraints();
rConstrain.setVgrow(Priority.SOMETIMES);
grid.getRowConstraints().add(rConstrain);
}
for (int i=0;i <5;i++)
{
for (int j=0;j <5;j++)
{
// create button and put it in an AnchorPane, that will resize it
AnchorPane buttonPane = new AnchorPane();
Button button = new Button("Button");
MaximizeInAnchorPane(button);
buttonPane.getChildren().add(button);
grid.add(buttonPane,i,j);
}
}
rootPane.getChildren().add(grid);
primaryStage.setTitle("test");
primaryStage.setScene(scene);
primaryStage.show();
}
private static void MaximizeInAnchorPane(Node toMaximize)
{
AnchorPane.setTopAnchor(toMaximize, 0.0);
AnchorPane.setRightAnchor(toMaximize, 0.0);
AnchorPane.setLeftAnchor(toMaximize, 0.0);
AnchorPane.setBottomAnchor(toMaximize, 0.0);
}
public static void main(String[] args) {
launch(args);
}
}
如果您尝试手动调整所有内容的大小,您应该看看 SplitPane。这完全取决于您要调整大小的方式。
对我来说,使用 JavaFX Scene Builder JavaFX Scene Builder 弄清楚如何做我想要的布局总是有帮助的。
【讨论】: