當我和javafx
瞎搞,我以前遇到過這樣一件事:
我創建了一個GridPane
,包裹在一個ScrollPane
和充滿Buttons
,但改變RowConstraints
和ColumnConstraints
爲預先提供GridPane
。
問題是水平滾動條看起來不足。我相信這是一個GridPane
胖,但它是怎麼發生的?
但是,垂直滾動條是完美的。JavaFX的GridPane在ScrollPane中是大於預期
sample.fxml
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
<children>
<ScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<GridPane fx:id="gridPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
</GridPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
sampleController.java
public class SampleController {
@FXML public GridPane gridPane;
@FXML
public void initialize(){
RowConstraints rowConstraints = new RowConstraints(10.0, 40, 40);
ColumnConstraints columnConstraints = new ColumnConstraints(10.0, 40, 40);
int i=0,j=0;
for(i=0; i<50; i++){
gridPane.getRowConstraints().add(i, rowConstraints);
for(j=0; j<30; j++) {
gridPane.getColumnConstraints().add(j, columnConstraints);
Button btn = new Button(Integer.toString(j+1));
btn.setPrefSize(40, 40);
gridPane.add(btn, j, i);
}
}
}
}
Dashboard.java
public class sample extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
}
擺脫RowConstraints和ColumnConstraints。你不需要它們。 – VGR