初始化方法导致应用程序启动方法异常

时间:2019-04-17 20:31:54

标签: java javafx fxml

这是我使用FXML的第一个项目,我的初始化方法和加载我的FXML文件时遇到问题。

我不确定我缺少什么,但是对该程序的任何帮助将不胜感激。

从控制器类摘录:

public class Controller implements Initializable {

    @FXML TextField input;
    @FXML TextArea output;
    @FXML RadioMenuItem chooseLonghand;
    @FXML RadioMenuItem chooseRoman;
    @FXML Button addInput;
    String number;
    Object conversion;
    Alert alert;
    BufferedNumberConverter converter;

    TextInputDialog retry = new TextInputDialog("Enter a valid integer");

    public Controller(){

    }

    @FXML
    public void initialize(URL url, ResourceBundle resourceBundle) {
        final ToggleGroup group = new ToggleGroup();
        chooseLonghand.setToggleGroup(group);
        chooseRoman.setToggleGroup(group);
    }

发射器类:

public class Launcher extends Application {
    public static void main(String [] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
        stage.setScene(new Scene(root));
        stage.setTitle("Number Converter");
        stage.show();

        // Close all windows when primaryStage closed
        stage.setOnCloseRequest(ev -> System.exit(0));

    }
}

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ragog.Controller">
   <children>
      <TextArea layoutY="200.0" prefHeight="200.0" prefWidth="600.0" />
      <Label layoutX="14.0" layoutY="14.0" prefHeight="17.0" prefWidth="91.0" text="Enter a number:" />
      <TextField layoutX="105.0" layoutY="10.0" />
      <Button layoutX="265.0" layoutY="10.0" mnemonicParsing="false" onAction="#addInput" text="Add Input" />
      <RadioButton layoutX="160.0" layoutY="56.0" mnemonicParsing="false" onAction="#chooseLonghand" text="Rewrite in longhand" />
      <RadioButton layoutX="160.0" layoutY="85.0" mnemonicParsing="false" onAction="#chooseRoman" prefHeight="17.0" prefWidth="177.0" text="Rewrite in roman numerals" />
      <Button layoutX="355.0" layoutY="81.0" mnemonicParsing="false" onAction="#rewrite" text="Rewrite!" />
      <Button layoutX="14.0" layoutY="158.0" mnemonicParsing="false" onAction="#output" text="Output:" />
   </children>
</Pane>

1 个答案:

答案 0 :(得分:0)

chooseLonghandchooseRoman在fxml文件中都没有相应的元素。您只能对onAction元素使用具有相同名称的<RadioButton>事件处理程序。 (我不确定为什么在FXMLLoader甚至到调用initialize之前都不会导致异常。)

因此,在调用null方法时,两个字段均为initialize

您需要将fx:id添加到应该注入到控制器的元素中:

<RadioButton fx:id="chooseLonghand" layoutX="160.0" layoutY="56.0" mnemonicParsing="false" onAction="#chooseLonghand" text="Rewrite in longhand" />
<RadioButton fx:id="chooseRoman" layoutX="160.0" layoutY="85.0" mnemonicParsing="false" onAction="#chooseRoman" prefHeight="17.0" prefWidth="177.0" text="Rewrite in roman numerals" />