Java FX让按钮工作

时间:2017-06-02 02:39:42

标签: java button javafx scenebuilder

我正在使用Scene Builder界面并通过fxml doc将其链接到Eclipse中。我的游戏背景和网格窗格/按钮显示正常。 但是如何让按钮工作? 我花了几天时间研究这个,虽然我确信这很简单! 基本上,我想要它做的就是当我点击按钮时显示数字1。我的游戏引擎本身工作正常,数字显示在TEXT 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());
        }
    }
}

} 以下是我的GUI类:

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);
}

} FXML文档(很长):

<?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>

0 个答案:

没有答案