gridPane中放两个按钮,居中显示,按照scene大小等比放大
时间: 2024-02-02 14:02:47 浏览: 32
您可以尝试以下代码实现您的需求:
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GridPaneExample extends Application {
@Override
public void start(Stage primaryStage) {
//创建GridPane布局
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
//创建两个按钮
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
//将按钮添加到GridPane中
gridPane.add(button1, 0, 0);
gridPane.add(button2, 1, 0);
//设置GridPane的列宽和行高,使得按钮等比放大
gridPane.getColumnConstraints().addAll(
new ColumnConstraints(50, 100, Double.MAX_VALUE),
new ColumnConstraints(50, 100, Double.MAX_VALUE)
);
gridPane.getRowConstraints().addAll(
new RowConstraints(50, 100, Double.MAX_VALUE)
);
//创建场景并将GridPane添加到场景中
Scene scene = new Scene(gridPane, 400, 400);
//设置场景大小变化时,GridPane中的元素等比例放大
gridPane.prefWidthProperty().bind(scene.widthProperty());
gridPane.prefHeightProperty().bind(scene.heightProperty());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
在上面的代码中,我们创建了一个`GridPane`布局,并将两个按钮添加到其中。然后,我们设置了`GridPane`的列宽和行高,使得按钮等比例放大。最后,我们创建了一个场景并将`GridPane`添加到场景中,并设置场景大小变化时,`GridPane`中的元素等比例放大。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)