JavaFX让按钮正常工作

JavaFX让按钮正常工作,java,button,javafx,scenebuilder,Java,Button,Javafx,Scenebuilder,我正在使用场景生成器界面,并通过fxml文档将其链接到Eclipse中。我的游戏背景和网格窗格/按钮显示良好。 但是我怎样才能让按钮工作呢? 我花了好几天的时间研究这个,虽然我相信它很简单! 基本上,当我点击按钮时,我想要它做的就是显示数字1。我的游戏引擎本身运行良好,数字显示在文本UI的网格上。 只是GUI吸引了我 public class MyController { private Maze game = new Maze(); private Label messages = new L

我正在使用场景生成器界面,并通过fxml文档将其链接到Eclipse中。我的游戏背景和网格窗格/按钮显示良好。 但是我怎样才能让按钮工作呢? 我花了好几天的时间研究这个,虽然我相信它很简单! 基本上,当我点击按钮时,我想要它做的就是显示数字1。我的游戏引擎本身运行良好,数字显示在文本UI的网格上。 只是GUI吸引了我

public class MyController {
private Maze game = new Maze();
private Label messages = new Label();
// private int moves = 0;

public void handleStart(ActionEvent event) {
    System.out.println("Welcome to The Maze.You must find all"
            + "+ the squares with zeros in them to make it out of the maze.");
}
public void handleHelp(ActionEvent event) {
    System.out.println("click on the ?, if you click on a square that is not 'zero'"
            + " you die. To win you must click on all the squares that are 'zero'. Good Luck! ");
}
@FXML
public void handleClick(ActionEvent event) {
    Button c = (Button) event.getSource();
    String id = c.getId();
    int row = Integer.parseInt(id.substring(6, 7));
    int col = Integer.parseInt(id.substring(7, 8));
    System.out.println("click: + " + event + "row=" + row + "," + col);
    boolean good = this.game.move1(row, col);
    if (good) {
        this.messages.setText("Good move");
    } else {
        this.messages.setText("Bad move.  Try again...");
    }
    updateView();
}
@FXML
GridPane grid;
public void updateView() {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 6; col++) {
            String bid = "#button" + row + col;
            Button b = (Button) grid.lookup(bid);
            System.out.println("Found Button: " + b);
            // Cell cell = game.getCell(row, col);
            // c.setText("" + cell.getValue());
        }
    }
}
} FXML文档(相当长):



有两种方法可以处理在FXML中创建的按钮。一种是通过使用
@FXML button buttonFXID
并添加
buttonFXID.setOnAction(…)
来获取按钮引用。另一种方法是使用按钮处理方法,该方法的启动方式类似于
@FXML public void handleClick(ActionEvent事件){..}
。在第一个示例中,还可以在Scenebuilder中设置按钮fxid。在第二个示例中,将
handleClick
或处理程序方法的名称添加到SceneBuilder.Post中按钮的onAction中。SceneBuilder基本上会为您更改FXML文件。塞德里克·杰斐逊,我已经在SceneBuilder中的按钮操作中添加了handleClick,这让我有点困惑。它应该有用吗?我也会发布FXML文件。您应该在方法之前添加
@FXML
。请看我上面评论中的handleClick。我刚刚这么做了,但它抛出了一个目标或空指针异常
public class GUI extends Application {
    private Maze game = new Maze();
    private int moves = 0;

    @Override // Override start method in Application Class
    public void start(Stage primaryStage) throws IOException {
    Parent root =FXMLLoader.load(getClass().getResource("themaze.fxml.fxml"));
    Scene scene = new Scene(root, 788, 522 );
    primaryStage.setResizable(false);
    primaryStage.setTitle("The Maze");
    primaryStage.setScene(scene);
    primaryStage.show();
}
public static void main(String[] args) {
    Application.launch(args);
}
<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="533.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maze_GUI.MyController">
<children>
  <ImageView fitHeight="533.0" fitWidth="800.0" layoutX="200.0" layoutY="116.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
     <image>
        <Image url="@images/maze.jpg.jpg" />
     </image>
  </ImageView>
  <GridPane layoutX="176.0" layoutY="155.0" prefHeight="327.0" prefWidth="561.0" AnchorPane.bottomAnchor="51.0" AnchorPane.leftAnchor="176.0" AnchorPane.rightAnchor="63.0" AnchorPane.topAnchor="155.0">
    <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    </columnConstraints>
    <rowConstraints>
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    </rowConstraints>
     <children>
        <Button fx:id="button25" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button15" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button05" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="5">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button24" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="118.0" prefWidth="103.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button14" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="122.0" prefWidth="113.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button23" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="120.0" prefWidth="107.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button22" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
            <Button fx:id="button21" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button12" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button11" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button20" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.rowIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button10" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="151.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button13" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="114.0" prefWidth="111.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3" GridPane.rowIndex="1">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button04" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="109.0" prefWidth="114.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="4">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button03" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="116.0" prefWidth="114.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="3">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button02" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="2">
           <font>
              <Font name="Century Schoolbook Bold" size="42.0" />
           </font>
        </Button>
        <Button fx:id="button01" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="121.0" prefWidth="93.0" style="-fx-border-color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9" GridPane.columnIndex="1">
               <font>
                  <Font name="Century Schoolbook Bold" size="42.0" />
               </font>
            </Button>
            <Button fx:id="button00" alignment="CENTER" mnemonicParsing="false" onAction="#handleClick" prefHeight="139.0" prefWidth="93.0" style="-fx-border color: white; -fx-border-width: 2; -fx-background-color: rgb(34, 139, 34);" text="?" textAlignment="CENTER" textFill="#fbf9f9">
               <font>
                  <Font name="Century Schoolbook Bold" size="42.0" />
               </font>
            </Button>
         </children>
      </GridPane>
   </children>
</AnchorPane>