1
我想讓我的GridPane修復列寬和行高。我想在每個單元格中顯示圖像,並將圖像調整到單元格的高度,寬度應調整大小並保持相同的比例(從未如此,圖像變得比單元格寬)。JavaFX GridPane固定列和行
經過一番搗鼓之後,我做了這樣的調整,使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不相等。
爲什麼你不只是iv.setFitHeight(SOME_FIXED_VALUE); –
與適合高度我可以設置一個固定的高度,但它應該使用大小,如果父母,在這種情況下,HBox。所以我試圖將圖像視圖的高度屬性綁定到外框的高度屬性。如果圖像較大,則不起作用,然後細胞被調整大小。如果圖像較小,則會調整大小。 – Rene8888
然後嘗試將hbox的高度設置爲固定值。 –