0

我的 JavaFX FXML 应用程序有一点问题。问题在于#handleButtonAction。由于某种原因,无法访问 Handler 方法。昨天还好好的,现在不知道怎么回事。有任何想法吗?你可以在这里查看整个应用程序:GitHub

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lotto.LottoNezetController">
    <children>
      <Button fx:id="button" layoutX="168.0" layoutY="234.0" onAction="#handleButtonAction" text="Sorsolás!" />
      <HBox layoutX="51.0" layoutY="95.0" spacing="30.0">
         <children>
            <TextField id="input1" fx:id="input1" layoutX="71.0" layoutY="88.0" prefHeight="25.0" prefWidth="36.0" />
            <TextField id="input2" fx:id="input2" layoutX="126.0" layoutY="88.0" prefHeight="25.0" prefWidth="36.0" />
            <TextField id="input4" fx:id="input4" layoutX="183.0" layoutY="88.0" prefHeight="25.0" prefWidth="36.0" />
            <TextField id="input3" fx:id="input3" layoutX="238.0" layoutY="88.0" prefHeight="25.0" prefWidth="36.0" />
            <TextField id="input5" fx:id="input5" layoutX="294.0" layoutY="88.0" prefHeight="25.0" prefWidth="36.0" />
         </children>
      </HBox>
      <HBox layoutX="20.0" layoutY="182.0" spacing="85.0">
         <children>
            <Label id="label1" fx:id="label1" layoutX="31.0" layoutY="100.0" />
            <Label id="label2" fx:id="label2" layoutX="86.0" layoutY="100.0" />
            <Label id="label3" fx:id="label3" layoutX="151.0" layoutY="100.0" />
            <Label id="label4" fx:id="label4" layoutX="202.0" layoutY="100.0" />
            <Label id="label5" fx:id="label5" layoutX="258.0" layoutY="100.0" prefHeight="17.0" prefWidth="21.0" />
         </children>
      </HBox>
      <Text layoutX="125.0" layoutY="40.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Lotto App">
         <font>
            <Font size="34.0" />
         </font>
      </Text>
      <Text layoutX="128.0" layoutY="75.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Irjon be 5 szamot 1tol 99ig." />
      <Label id="eredmeny" fx:id="eredmeny" layoutX="179.0" layoutY="217.0" prefHeight="17.0" prefWidth="43.0" />
    </children>
</AnchorPane>

控制器类

package lotto;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class LottoNezetController implements Initializable {

    private final int MAX = 99; // finalt nagybetuvel illik, nevadasi konvencio
    private final int MIN = 1;

    private int genNum1;
    private int genNum2;
    private int genNum3;
    private int genNum4;
    private int genNum5;

    private int selNum1;
    private int selNum2;
    private int selNum3;
    private int selNum4;
    private int selNum5;

    @FXML
    private Label label1; 
    @FXML
    private Label label2;
    @FXML
    private Label label3;
    @FXML
    private Label label4;
    @FXML
    private Label label5;

    @FXML
    private TextField input1; 
    @FXML
    private TextField input2;
    @FXML
    private TextField input3;
    @FXML
    private TextField input4;
    @FXML
    private TextField input5;

    @FXML
    private Label eredmeny;

    private void handleButtonAction(ActionEvent event) {

        genNum1 = 0;
        genNum2 = 0;
        genNum3 = 0;
        genNum4 = 0;
        genNum5 = 0;

        genNum1 = getRandomNumber();
        genNum2 = getRandomNumber();
        genNum3 = getRandomNumber();
        genNum4 = getRandomNumber();
        genNum5 = getRandomNumber();

        label1.setText(String.valueOf(genNum1));
        label2.setText(String.valueOf(genNum2));
        label3.setText(String.valueOf(genNum3));
        label4.setText(String.valueOf(genNum4));
        label5.setText(String.valueOf(genNum5));

        calculate();
    }

    private int getRandomNumber()
    {
        int random = (int) (Math.random() * MAX) + MIN;

        if (random == genNum1 || random == genNum2 || random == genNum3 || random == genNum4 || random == genNum5)
        {
            return getRandomNumber();
        }

        return random;
    }

    private String calculate()
    {
        try
        {
            selNum1 = Integer.parseInt(input1.getText());
            selNum2 = Integer.parseInt(input2.getText());
            selNum3 = Integer.parseInt(input3.getText());
            selNum4 = Integer.parseInt(input4.getText());
            selNum5 = Integer.parseInt(input5.getText());
        }
        catch(Exception e)
        {
             System.out.println("valami");       
        }
        String toReturn = " teszt";

        return toReturn;
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //egyszer tuti lefut
    }    

}
4

1 回答 1

1

正如错误消息所述,您的处理程序方法不可访问,因为它既没有public也没有@FXML注释。添加注释:

@FXML
private void handleButtonAction(ActionEvent event) {

    genNum1 = 0;
    genNum2 = 0;
    genNum3 = 0;
    genNum4 = 0;
    genNum5 = 0;

    genNum1 = getRandomNumber();
    genNum2 = getRandomNumber();
    genNum3 = getRandomNumber();
    genNum4 = getRandomNumber();
    genNum5 = getRandomNumber();

    label1.setText(String.valueOf(genNum1));
    label2.setText(String.valueOf(genNum2));
    label3.setText(String.valueOf(genNum3));
    label4.setText(String.valueOf(genNum4));
    label5.setText(String.valueOf(genNum5));

    calculate();
}
于 2017-11-12T19:29:40.387 回答