用JavaFX按钮填充整个场景
我正在尝试在VBox上工作,我希望用最多5个包含最多5个按钮的HBox填充它。 此外,我需要这些按钮的大小,但我无法做到这一点。 已经尝试使用静态方法返回HBox,从用JavaFX按钮填充整个场景,java,javafx,Java,Javafx,我正在尝试在VBox上工作,我希望用最多5个包含最多5个按钮的HBox填充它。 此外,我需要这些按钮的大小,但我无法做到这一点。 已经尝试使用静态方法返回HBox,从 primaryStage.getChildren.addAll(hBoxMethod()); 非常感谢您的帮助:)不太清楚您想做什么。但是如果你想用5x5自动调整大小的按钮(作为标题对象)填充你的屏幕,为什么不使用GridPane和AnchorPanes的组合来调整大小呢 package sample; import java
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);
}
}
包装样品;
导入javafx.application.application;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.*;
导入javafx.stage.stage;
公共类主扩展应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
锚烷根窗格=新锚烷();
场景=新场景(根窗格,400300);
GridPane grid=新建GridPane();
最大化中空玻璃(网格);
//调整列和行的大小
对于(int i=0;我请告诉我们您有哪些选择。每个按钮代表商店提供的产品,位于数据库中,分类。例如,销售人员选择销售苏打水,然后屏幕上会显示商店所有的苏打水,如可乐、雪碧等。问题是,不会总是有25种产品,但我如果超过25个,则将其设置为一个限制,除非它们没有很好地分类,否则这是不太可能的。感谢您的回答,我将尝试为操作取消嵌套,以便仅添加所需的按钮数。