我已将问题简化为问题,所以我在下面的控制器中的StartGame方法中为扫雷游戏制作了一个UI,一切正常(根据我使用debug的检查),直到
game.open(x,y)
,这意味着我设法创建板并向GridPane添加按钮(将用作插槽),但是我的问题是我似乎无法打开并在插槽上显示正确的值。 (Mines类的逻辑工作正常-没有javaFX部分的逻辑。)
package MS;
import java.io.IOException;
import java.util.Random;
import javafx.event.actionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class NewGameCONTROLLER {
Mines game;
int rows,columns,mines;
Button b;
@FXML
private TextField NumRows;
@FXML
private TextField NumCols;
@FXML
private TextField NumM;
@FXML
private Button StartGame;
@FXML
private Button BackMainmenu;
@FXML
private Button RandomGame;
@FXML
void BackMainmenu(actionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getclass().getResource("MainmenuFXML.fxml")); // fxml location
VBox root = loader.load(); // Load layout
root.setStyle("-fx-background-image: url(\"file:///C:/eclipseProjects/Minesweeper/src/MS/menu.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage primaryStage = (Stage) ((Node) event.getsource()).getScene().getWindow();
primaryStage.setTitle("..."); // Set stage's title
primaryStage.setMinWidth(400); // Won't be allowed to make width/height smaller
primaryStage.setMinHeight(350);
primaryStage.setMaxWidth(600);
primaryStage.setMaxHeight(450);
primaryStage.setScene(scene); // Set scene to stage
primaryStage.show(); // Show stage
}
@FXML
void RandomGame(actionEvent event) {
Random rand = new Random();
int low = 3,high = 15;
int minesLow = 1,minesHigh;
rows = rand.nextInt(high - low) + low;
columns = rand.nextInt(high - low) + low;
minesHigh = rows * columns - 1;
mines = rand.nextInt(minesHigh - minesLow) + minesLow;
game = new Mines(rows,mines);
}
@FXML
void StartGame(actionEvent event) throws IOException {
rows = Integer.parseInt(NumRows.getText());
columns = Integer.parseInt(NumCols.getText());
mines = Integer.parseInt(NumM.getText());
game = new Mines(rows,mines);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getclass().getResource("BoardFXML.fxml")); // fxml location
AnchorPane root = loader.load(); // Load layout
root.setStyle("-fx-background-image: url(\"file:///C:/eclipseProjects/Minesweeper/src/MS/Pic.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage gameStage = (Stage) ((Node) event.getsource()).getScene().getWindow();
gameStage.setTitle("..."); // Set stage's title
gameStage.setMinWidth(1000); // Won't be allowed to make width/height smaller
gameStage.setMinHeight(500);
gameStage.setMaxWidth(1200);
gameStage.setMaxHeight(800);
gameStage.setScene(scene); // Set scene to stage
BoardCONTROLLER bCont = loader.getcontroller(); // Prepare board in BoardCONTROLLER
for (int i = 0; i < columns; i++)
for (int j = 0; j < rows; j++) {
b = new Button(" ");
b.setMinSize(40,40);
b.setMaxSize(40,40);
bCont.TheBoard.add(b,i,j);
}
for (int i = 0; i < bCont.TheBoard.getchildren().size(); i++) {
((ButtonBase) bCont.TheBoard.getchildren().get(i)).setOnaction(new EventHandler<actionEvent>() {
@Override
public void handle(actionEvent event) {
int x,y;
event.getsource();
x = (int) ((Button) event.getsource()).getProperties().get("gridpane-column");
y = (int) ((Button) event.getsource()).getProperties().get("gridpane-row");
game.open(x,y);
for (int i=0;i<game.getcol();i++)
for (int j=0;i<game.getRow();j++) {
if (game.board[i][j].charAt(1)=='T')
((Button) bCont.TheBoard.getchildren().get(i)).setText(game.get(x,y));
}
/* while ()
for (int i = 0; i < bCont.TheBoard.getchildren().size(); i++) {
if (game.board[x][y].charAt(2) == 'B') {
((Button) bCont.TheBoard.getchildren().get(i)).setText(game.get(x,y));
}
}*/
}
});
}
gameStage.show(); // Show stage
}
}
谢谢。