为什么在Java中会出现javafx.fxml.LoadException
我正在为我的学校项目制作数独游戏,需要打开一个新窗口,在那里列出高分,但是每次我点击菜单项,控制台就会抛出错误,我不知道我能做什么 我已经试着在互联网上寻找一些东西,但我所做的一切都导致了同样的错误为什么在Java中会出现javafx.fxml.LoadException,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我正在为我的学校项目制作数独游戏,需要打开一个新窗口,在那里列出高分,但是每次我点击菜单项,控制台就会抛出错误,我不知道我能做什么 我已经试着在互联网上寻找一些东西,但我所做的一切都导致了同样的错误 package controller; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.event.EventHandler; import javafx
package controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import model.GameBoard;
public class Controller implements Initializable {
@FXML
Button button_one;
@FXML
Button button_two;
@FXML
Button button_three;
@FXML
Button button_four;
@FXML
Button button_five;
@FXML
Button button_six;
@FXML
Button button_seven;
@FXML
Button button_eight;
@FXML
Button button_nine;
@FXML
Canvas canvas;
GameBoard gameboard;
int psr; // ausgewählte reihe
int psc; // ausgewählte spalte
@Override
public void initialize(URL location, ResourceBundle resources) {
gameboard = new GameBoard();
psr = 0;
psc = 0;
GraphicsContext context = canvas.getGraphicsContext2D();
try {
drawOnCanvas(context);
} catch (IOException e) {
e.printStackTrace();
}
}
public void drawOnCanvas(GraphicsContext context) throws IOException {
context.clearRect(0, 0, 450, 450);
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int posy = row * 50 + 2;
int posx = col * 50 + 2;
int width = 46;
context.setFill(Color.LIGHTGRAY);
context.fillRoundRect(posx, posy, width, width,10,10);
}
}
context.setStroke(Color.RED);
context.setLineWidth(2);
context.strokeRoundRect(psc * 50 + 2, psr * 50 + 2, 46, 46, 10, 10);
int[][] initial = gameboard.getInitial();
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int posy = row * 50 + 30;
int posx = col * 50 + 20;
context.setFill(Color.BLACK);
context.setFont(new Font(20));
if (initial[row][col] != 0) {
context.fillText(initial[row][col] + "", posx, posy);
}
}
}
int[][] player = gameboard.getPlayer();
for(int row = 0; row < 9;row++) {
for(int col = 0; col < 9; col++) {
int posy = row * 50 + 30;
int posx = col * 50 + 20;
context.setFill(Color.RED);
context.setFont(new Font(20));
if(player[row][col] != 0) {
context.fillText(player[row][col] + "", posx, posy);
}
}
}
//Die vier Linien für die 3x3 Trennung
context.setStroke(Color.BLACK);
context.strokeLine(450, 150, 0, 150);
context.strokeLine(450, 300, 0, 300);
context.strokeLine(150, 0, 150, 450);
context.strokeLine(300, 0, 300, 450);
context.setLineWidth(3);
if(gameboard.checkSuccess()) {
context.clearRect(0, 0, 450, 450);
context.setFill(Color.GREEN);
context.setFont(new Font(36));
context.fillText("SUPER!", 150, 200);
newWindow();
}
}
public void canvasMouseClick() throws IOException{
canvas.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int mousex = (int) event.getX();
int mousey = (int) event.getY();
psr = (int) (mousey / 50);
psc = (int) (mousex / 50);
// System.out.println(psr + "," + psc);
try {
drawOnCanvas(canvas.getGraphicsContext2D());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void button1pressed() throws IOException{
gameboard.modifyPlayer(1, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button2pressed() throws IOException{
gameboard.modifyPlayer(2, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button3pressed() throws IOException{
gameboard.modifyPlayer(3, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button4pressed() throws IOException{
gameboard.modifyPlayer(4, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button5pressed() throws IOException{
gameboard.modifyPlayer(5, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button6pressed() throws IOException{
gameboard.modifyPlayer(6, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button7pressed() throws IOException{
gameboard.modifyPlayer(7, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button8pressed() throws IOException {
gameboard.modifyPlayer(8, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button9pressed() throws IOException {
gameboard.modifyPlayer(9, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void neuesSpiel() throws IOException {
gameboard = new GameBoard();
psr = 0;
psc = 0;
GraphicsContext context = canvas.getGraphicsContext2D();
drawOnCanvas(context);
}
public void newWindow() throws IOException {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/controller/highscore.fxml"));
VBox root1 = (VBox) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
表明错误在控制器类的第58行。这似乎是这一行:
GraphicsContext context = canvas.getGraphicsContext2D();
如果是这样,那么如果canvas
为空,就会发生这种情况
通过确保正确实例化了canvas
,您很可能能够解决此错误,但如果需要进一步的帮助,您可以阅读更多内容
表明错误在控制器类的第58行。这似乎是这一行:
GraphicsContext context = canvas.getGraphicsContext2D();
如果是这样,那么如果canvas
为空,就会发生这种情况
通过确保正确实例化了
canvas
,您很可能能够解决错误,但如果需要进一步帮助,您可以阅读更多。您的canvas
在FXML文件中是否有fx:id=“canvas”
属性?您的canvas
是否有fx:id=“canvas”
FXML文件中的属性?
GraphicsContext context = canvas.getGraphicsContext2D();