java中gu界面固定大小i_在JavaFX中将元素动态添加到固定大小的GridPane

似乎TilePane比GridPane更适合此用例。

uiBBu.png

CDLHg.png

import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.TilePane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

// java 8 code

public class DynamicTiles extends Application {

//Class containing grid (see below)

private GridDisplay gridDisplay;

//Class responsible for displaying the grid containing the Rectangles

public class GridDisplay {

private static final double ELEMENT_SIZE = 100;

private static final double GAP = ELEMENT_SIZE / 10;

private TilePane tilePane = new TilePane();

private Group display = new Group(tilePane);

private int nRows;

private int nCols;

public GridDisplay(int nRows, int nCols) {

tilePane.setStyle("-fx-background-color: rgba(255, 215, 0, 0.1);");

tilePane.setHgap(GAP);

tilePane.setVgap(GAP);

setColumns(nCols);

setRows(nRows);

}

public void setColumns(int newColumns) {

nCols = newColumns;

tilePane.setPrefColumns(nCols);

createElements();

}

public void setRows(int newRows) {

nRows = newRows;

tilePane.setPrefRows(nRows);

createElements();

}

public Group getDisplay() {

return display;

}

private void createElements() {

tilePane.getChildren().clear();

for (int i = 0; i < nCols; i++) {

for (int j = 0; j < nRows; j++) {

tilePane.getChildren().add(createElement());

}

}

}

private Rectangle createElement() {

Rectangle rectangle = new Rectangle(ELEMENT_SIZE, ELEMENT_SIZE);

rectangle.setStroke(Color.ORANGE);

rectangle.setFill(Color.STEELBLUE);

return rectangle;

}

}

@Override

public void start(Stage primaryStage) {

//Represents the grid with Rectangles

gridDisplay = new GridDisplay(2, 4);

//Fields to specify number of rows/columns

TextField rowField = new TextField("2");

TextField columnField = new TextField("4");

//Function to set an action when text field loses focus

buildTextFieldActions(rowField, columnField);

HBox fields = new HBox(10);

fields.getChildren().add(rowField);

fields.getChildren().add(new Label("x"));

fields.getChildren().add(columnField);

BorderPane mainPanel = new BorderPane();

mainPanel.setCenter(gridDisplay.getDisplay());

mainPanel.setTop(fields);

Scene scene = new Scene(mainPanel, 1000, 800);

primaryStage.setTitle("Test grid display");

primaryStage.setScene(scene);

primaryStage.show();

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

launch(args);

}

private void buildTextFieldActions(final TextField rowField, final TextField columnField) {

rowField.focusedProperty().addListener((ov, t, t1) -> {

if (!t1) {

if (!rowField.getText().equals("")) {

try {

int nbRow = Integer.parseInt(rowField.getText());

gridDisplay.setRows(nbRow);

} catch (NumberFormatException nfe) {

System.out.println("Please enter a valid number.");

}

}

}

});

columnField.focusedProperty().addListener((ov, t, t1) -> {

if (!t1) {

if (!columnField.getText().equals("")) {

try {

int nbColumn = Integer.parseInt(columnField.getText());

gridDisplay.setColumns(nbColumn);

} catch (NumberFormatException nfe) {

System.out.println("Please enter a valid number.");

}

}

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值