我一直在使用FXML處理javafx程序。我試圖從一個場景中拉入的信息動態創建一個TilePane,我似乎無法讓TilePane出現。我嘗試過所有我能想到的事情,而且我很難過。我在下面發佈相關代碼。感謝您的幫助!無法創建動態創建的JavaFX TilePane
編輯:我簡化了下面的代碼以方便閱讀。如果這還不夠,我會製作一個MCVE。
主控制器
@FXML private ScrollPane gridScroll;
private TilePane tPane;
//Method to build each cell for the tilepane
private StackPane buildCell(Stitch stitch){
StackPane pane = new StackPane();
pane.setPrefSize(5, 5);
// add labels to stackpane
Label stitchLabel = new Label(stitch.getStitchType().toString());
Label strandLabel = new Label(stitch.getNumStrands().toString());
//create rectangle to color stackpane
Rectangle rect = new Rectangle (5, 5); //set rectangle to same size as stackpane
rect.setFill(stitch.getDisplayColor()); //Color the rectangle
rect.setStroke(Color.BLACK); //border the rectangle in black
pane.getChildren().addAll(stitchLabel, strandLabel, rect);
return pane;
}
protected void createTiles(Stitch[][] stitchArray, int width, int height){
tPane = new TilePane(); //Create a new tilepane
gridScroll.setContent(tPane); //add tilepane to existing scrollpane
tPane.setPrefColumns(width); //set prefcolumns to the array width
//add cells to tilepane
for (int i=0; i<width; i++){
for (int j=0; j<height; j++){
StackPane cell = buildCell(stitchArray[i][j]);
tPane.getChildren().add(cell);
}
}
}
從調用tilepane副控制器編碼創建
@FXML void finish(){
//create data to populate tilepane with
Stitch[][] stitchArray = floss.ImageConversion.createStitchArray(posterizedImage);
int width = (int) currentPicWidth; //cast double to int
int height = (int) currentPicHeight; //cast double to int
//get the main Controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("InStitchesFXML.fxml"));
try {
loader.load();
} catch (IOException e) {e.printStackTrace();}
InStitchesController isCtrl = loader.getController();
//create the tiles
isCtrl.createTiles(stitchArray, width, height);
//close the stage
importStage.close();
}
我希望這有助於澄清事情。
可能重複的[JavaFX FileChooser拋出錯誤(可能易於修復,但仍然困惑)](http://stackoverflow.com/questions/23094846/javafx-filechooser-throws-error-probably-easy-fix-but-仍然困惑)。另請參閱http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields – 2014-09-22 20:32:53
從該線程實施建議,但我仍然有問題。該帖子已更新以反映它們。 – 2014-09-22 21:48:40
我實際上看不到您的更新版本有任何問題,但很難遵循,代碼也不完整。也許你可以創建一個[MCVE](http://stackoverflow.com/help/mcve),它可以完成你正在描述的內容(例如一個帶有按鈕和平鋪窗格的平臺,它將打開一個新的FXML並填充平鋪窗格)並沒有更多,並張貼。至少,刪除所有從未引用的字段,例如'InStitchesController'中的'primaryStage'和'stitchArray'。 – 2014-09-23 00:22:08