【发布时间】:2019-05-11 12:30:04
【问题描述】:
我正在创建一个应用程序,该应用程序在 gridPane 的每个单元格中随机显示(不同颜色的)圆圈。
我想要做的是创建一个“随机播放”按钮,该按钮在 gridPane 中随机更改每个圆圈的位置。但是,我总是遇到一堆问题。
这是我到目前为止所拥有的。我的两个类(没有添加 XML 文件):
控制器类
public class viewController {
//My two Variables, a gridPane and a button
@FXML
private GridPane matrix;
@FXML
private Button shuffleBut;
//my eventHandler event that should (1) add circles to all the cells, and
(2) shuffle them amongst the cells in the gridPane.
void shuffle(ActionEvent e) {
Random r = new Random ();
int rowShuffle = r.next((4-0)+1);
int colShuffle = r.next((4-0)+1);
Circle newCircle = new Circle ();
matrix.add(newCircle, rowShuffle, colShuffle );
}
主类
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// just load fxml file and display it in the stage:
Parent root = FXMLLoader.Load(getClass().getResource("mainUI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
// main method to support non-JavaFX-aware environments:
public static void main(String[] args) {
// starts the FX toolkit, instantiates this class,
// and calls start(...) on the FX Application thread:
launch(args);
}
【问题讨论】:
-
创建一个
ArrayList圈子。随机播放List。将列表添加到GridPane。需要重新洗牌吗?从GridPane中删除Circles。随机播放List。将Circles添加回GridPane。