In meinem Code habe ich einen Event-Handler erstellt, der ursprünglich funktioniert hat; aber nach dem Hinzufügen von mehr Code funktioniert es nicht mehr, so dass ich vor meinen Änderungen wiederhergestellt habe und es immer noch nicht funktioniert. Gibt es etwas, was ich falsch gemacht habe? Außerdem verwende ich Eclipse, wenn das bei der Bestimmung einer Ursache hilfreich ist.JavaFX Event Handler funktioniert nicht mehr
Main.java: Paketanwendung;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.InputEvent;
import javafx.scene.input.MouseEvent;
public class Main extends Application {
public Parent root;
public ChessUtil chess = new ChessUtil();
@Override
public void start(Stage primaryStage) {
try {
root = FXMLLoader.load(getClass().getResource("/Chess.fxml"));
Node peice;
for(int x = 0; x < 16 ;x++)
{
peice = root.lookup("#" + chess.whiteID[x]);
peice.addEventHandler(MouseEvent.MOUSE_CLICKED, peiceClick);
peice = root.lookup("#" + chess.blackID[x]);
peice.addEventHandler(MouseEvent.MOUSE_CLICKED, peiceClick);
}
disablePeicesToggle(chess.blackID, true);
Scene scene = new Scene(root,740,740);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
EventHandler peiceClick = new EventHandler<InputEvent>() {
public void handle(InputEvent event) {
String id = ((Node) event.getSource()).getId();
ArrayList<String> moves = chess.findPeiceMoves(id);
System.out.println(moves.size());
if(moves.size() > 0)
{
System.out.println(moves);
}
System.out.println("Handling event " + event.getEventType());
event.consume();
}
};
public void disablePeicesToggle(String[] idSet, boolean disable)
{
Node peice;
for(int x = 0; x < 16 ;x++)
{
peice = root.lookup("#" + idSet[x]);
peice.setDisable(disable);
}
}
public static void main(String[] args) {
launch(args);
}
}
Chess.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="740.0" prefWidth="740.0" stylesheets="@application/application.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane id="chessboard" layoutX="10.0" layoutY="10.0" prefHeight="720.0" prefWidth="720.0" stylesheets="@application/application.css" />
<AnchorPane id="board-spaces" layoutX="50.0" layoutY="50.0" prefHeight="640.0" prefWidth="640.0">
<children>
<Region id="b7" prefHeight="80.0" prefWidth="80.0" styleClass="bRook" />
<Region id="b3" layoutX="160.0" prefHeight="80.0" prefWidth="80.0" styleClass="bBishop" />
<Region id="b5" layoutX="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bKnight" />
<Region id="b2" layoutX="240.0" prefHeight="80.0" prefWidth="80.0" styleClass="bQueen" />
<Region id="b1" layoutX="320.0" prefHeight="80.0" prefWidth="80.0" styleClass="bKing" />
<Region id="b4" layoutX="400.0" prefHeight="80.0" prefWidth="80.0" styleClass="bBishop" />
<Region id="b6" layoutX="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="bKnight" />
<Region id="b8" layoutX="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="bRook" />
<Region id="b12" layoutX="240.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b10" layoutX="80.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b13" layoutX="320.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b16" layoutX="560.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b9" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b15" layoutX="480.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b11" layoutX="160.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="b14" layoutX="400.0" layoutY="80.0" prefHeight="80.0" prefWidth="80.0" styleClass="bPawn" />
<Region id="w3" layoutX="160.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wBishop" />
<Region id="w12" layoutX="240.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w10" layoutX="80.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w5" layoutX="80.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wKnight" />
<Region id="w7" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wRook" />
<Region id="w14" layoutX="400.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w1" layoutX="320.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wKing" />
<Region id="w2" layoutX="240.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wQueen" />
<Region id="w6" layoutX="480.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wKnight" />
<Region id="w13" layoutX="320.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w16" layoutX="560.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w9" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w15" layoutX="480.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w11" layoutX="160.0" layoutY="480.0" prefHeight="80.0" prefWidth="80.0" styleClass="wPawn" />
<Region id="w8" layoutX="560.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wRook" />
<Region id="w4" layoutX="400.0" layoutY="560.0" prefHeight="80.0" prefWidth="80.0" styleClass="wBishop" />
</children></AnchorPane>
<AnchorPane id="board-overlay" layoutX="60.0" layoutY="60.0" prefHeight="640.0" prefWidth="640.0" />
</children>
</AnchorPane>
Update: Ich habe meinen Code in einem GitHub Repo setzen. Der vollständige Quellcode ist unter github.com/stitch366/chess/tree/master/chess.
Nein.Was ich suche ist bereits in der fxml – stitch366
Full Source Code ist unter https://github.com/stitch366/chess/tree/master/chess. Außerdem lade ich eine FXML-Datei, und die IDs wurden in der FXML-Datei festgelegt. Es gibt keinen Controller, da ich keine benutzerdefinierte Komponente verwende oder Daten nicht anzeigen kann. – stitch366
Ich habe meinen Code auf GitHub gesetzt, was bedeutet, dass Sie ihn einschließlich der FXML betrachten können; Das Hinzufügen der FXML würde den Weg länger machen, als es bereits ist. Außerdem funktioniert ein Controller nicht für meine Anwendung. – stitch366