我想让我的GridPane修复列宽和行高。我想在每个单元格中显示一个图像,并将图像调整大小到单元格的高度,宽度应该调整大小并保持相同的比例(从来没有这种情况,图像比单元格更宽)。 / p>
经过大量的摆弄后,我做了3列和10行,并将其调整为父级大小。我无法提供一个工作示例,但这里是相关代码:
public class JavaFXTest extends Application {
private static final int screenWidth = 1920 / 2;
private static final int screenHeight = 1080 / 2;
private static final Color backgroundColor = Color.BLACK;
private static final Color cellBackgroundColor = Color.DARKGRAY;
private static final Color textColor = Color.WHITE;
private static final Font standartFont = Font.font("Arial", FontWeight.NORMAL, 18);
private static final int columnCount = 3;
private static final int rowCount = 10;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws JSONException, FileNotFoundException, IOException {
primaryStage.setTitle("Drawing Operations Test");
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setWidth(screenWidth);
primaryStage.setHeight(screenHeight);
primaryStage.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.C) {
Platform.exit();
}
}
});
Group root = new Group();
root.setCursor(Cursor.NONE);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10, 10, 10, 10));
grid.prefWidthProperty().bind(primaryStage.widthProperty());
grid.prefHeightProperty().bind(primaryStage.heightProperty());
grid.setBackground(new Background(new BackgroundFill(backgroundColor, null, null)));
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
for (int i = 0; i < columnCount; i++) {
grid.getColumnConstraints().add(cc);
}
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
for (int i = 0; i < rowCount; i++) {
grid.getRowConstraints().add(rc);
}
JSONArray arr = new JSONArray(Util.StreamToString(new FileInputStream(new File("countries.json"))));
int counter = 0;
for (int i = 0; i < columnCount; i++) {
for (int j = 0; j < rowCount; j++) {
HBox box = new HBox();
box.setBackground(new Background(new BackgroundFill(cellBackgroundColor, null, null)));
JSONObject curr = arr.getJSONObject(counter);
String code = curr.getString("code");
String name = curr.getString("name");
File img = new File("flag_img/" + code.toUpperCase() + ".png");
if (img.exists()) {
ImageView iv = new ImageView(new Image(new FileInputStream(img)));
iv.setPreserveRatio(true);
iv.setSmooth(true);
iv.setCache(true);
iv.fitHeightProperty().bind(box.heightProperty());
box.getChildren().add(iv);
}
Text text = new Text(name);
text.setFont(standartFont);
text.setFill(textColor);
box.getChildren().add(text);
grid.add(box, i, j);
counter++;
}
}
root.getChildren().add(grid);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
以下是两个截图: 第一个显示没有图像的布局:
第二个是与上面完全相同的布局,只是图像
也许你可以告诉我,为什么这些列与HBox不相同。
答案 0 :(得分:4)
我通过计算图像和标签的宽度和高度来解决问题。让它恰当地适应是非常困难的,但是一旦完成它就会非常好用