私のコードでは、もともと働いていたイベントハンドラを作成しました。ただし、コードを追加した後は機能しなくなり、変更前に元に戻ってしまい、それでも機能しません。私が間違っていることはありますか?また、原因を特定するのに役立つなら、私はeclipseを使っています。JavaFXイベントハンドラが機能しなくなった
Main.java: パッケージアプリケーション。
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>
アップデート:私はGitHubのレポで私のコードを入れています。 完全なソースコードはgithub.com/stitch366/chess/tree/master/chessにあります。
No.私が探しているものはすでにfxmlにあります – stitch366
フルソースコードはhttps://github.com/stitch366/chess/tree/master/chessにあります。また、私はFXMLファイルを読み込んでおり、IDはFXMLファイルに設定されています。私はカスタムコンポーネントを使用していないか、データを表示していないため、コントローラはありません。 – stitch366
私はGitHubに自分のコードを書いています。つまり、FXMLを含めて見ることができます。 FXMLを追加すると、それよりもずっと長くなります。また、コントローラは私のアプリケーションでは動作しません。 – stitch366