我有一个带有for循环的对象,它将数组元素的内容切换为用户指定的次数。我现在可以在每个循环结束时将更新的数组输出到Eclipse控制台,但我现在必须在GUI窗口内显示更新的数组。 我已经有了工作代码来显示窗口和带有项目的菜单栏,但我不知道如何让数组在每个循环结束时在网格窗格中显示它的元素。 我应该用矩形填充GridPane(一个用于数组的每个元素),然后移动矩形以反映每个循环结束时数组中的变化。我的代码目前如下:
public void start(Stage stagePrimary) throws Exception {
stagePrimary.setTitle("My Application");
//Sets the window title.
Scene scenePrimary = new Scene(new VBox(), 500, 500);
// Creates the scene.
// CODE FOR MENU BAR IS HERE
KeyFrame KFSim = new KeyFrame(Duration.millis(1000),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent AE1) {
/* Simulation stuff (i.e. updating the array)
is supposed to go here. */
Platform.runLater(new Runnable() {
@Override
public void run() {
//Gridpane is supposed to go here.
}
});
}
});
stagePrimary.setScene(scenePrimary);
stagePrimary.show();
包含my数组的类位于名为SimInstance的类中:
public class SimInstance{
//Irrelevant variables.
int iNumLoops
//The number of loops which the simulation should make.
private char[][] cEditableMap;
//Array will be updated throughout the simulation.
public void main(){
for (int iLoopCount = 0; iLoopCount < iNumLoops; iLoopCount++){
UpdateMap();
//Updates the cEditableMap with the new positions.
PrintMap();
//Prints the updated map to the Eclipse Console.
}
}
}
我目前没有设置GridPane或Rectangle对象。
答案 0 :(得分:0)
如果我理解你的问题:
我会提供一些示例代码,但在此之前您应该先关注一些在线教程。
TilePane pane = new TilePane();
for (String s : array){
Label label = new Label(s, new Rectangle(height, width));
pane.getChildren().add(label);
}
或:
GridPane pane = new GridPane();
for (int x = 0; x < array.length; x++){
for (int y = 0; y < array[x].length; y++){
Label label = new Label(array[x][y], new Rectangle(height, width));
pane.add(label, x, y);
}
}