我正在编写一个小应用程序(一个简单的计算器)用于演示目的,我在下面的课程中遇到了一些问题。每当我尝试运行应用程序时,我都会遇到由initialize()中的按钮引起的InvocationTargetException。调用按钮的任何方法都会重现问题。
非常感谢任何帮助。
package ui;
import java.io.IOException;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
public class CalculatorController extends GridPane {
@FXML
private TextArea displayArea;
@FXML
private Button mcButton, mrButton, msButton, mPlusButton, mMinusButton,
deleteButton, clearEverythingButton, clearButton, negateButton,
sqrtButton, sevenButton, eightButton, nineButton, divideButton,
percentButton, fourButton, fiveButton, sixButton, multiplyButton,
reciprocButton, oneButton, twoButton, threeButton, minusButton,
resultButton, zeroButton, fractionButton, plusButton;
@SuppressWarnings("LeakingThisInConstructor")
public CalculatorController() {
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Calculator.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
System.out.println(e);
Platform.exit();
}
}
@FXML
private void initialize() {
// do something with the buttons here to reproduce exception
// e.g. call any of their methods
}
}
我的主要课程:
package main;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import ui.CalculatorController;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Calculator");
Parent root = new CalculatorController();
stage.setScene(new Scene(root, 217, 306));
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最后是fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<fx:root type="javafx.scene.layout.GridPane" alignment="CENTER" hgap="2.0" prefHeight="306.0" prefWidth="217.0" vgap="2.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<Button fx:id="mcButtton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="MC" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Button fx:id="mrButtton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="MR" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="msButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="MS" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Button fx:id="mPlusButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="M+" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Button fx:id="mMinusButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="M-" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="2" />
<Button fx:id="deleteButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="⬅" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="3">
<font>
<Font size="14.0" />
</font>
</Button>
<Button fx:id="clearEverythingButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="CE" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button fx:id="clearButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="C" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Button fx:id="negateButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="±" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<Button fx:id="sqrtButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="√" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="3" />
<Button fx:id="sevenButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="7" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="4" />
<Button fx:id="eightButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="8" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Button fx:id="nineButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="9" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<Button fx:id="divideButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="/" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="4" />
<Button fx:id="percentButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="\%" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="4" />
<Button fx:id="fourButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="4" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="5" />
<Button fx:id="fiveButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="5" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button fx:id="sixButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="6" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="5" />
<Button fx:id="multiplyButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="*" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button fx:id="reciprocButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="1/x" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="5" />
<Button fx:id="oneButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="1" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.rowIndex="6" />
<Button fx:id="twoButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="2" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<Button fx:id="threeButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="3" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="6" />
<Button fx:id="minusButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="-" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="6" />
<Button fx:id="zeroButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="0" textAlignment="CENTER" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="7" />
<Button fx:id="fractionButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="," textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<Button fx:id="plusButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="+" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="7" />
<Button fx:id="resultButton" alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="=" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="6" GridPane.rowSpan="2" />
<TextArea fx:id="displayArea" prefWidth="200.0" wrapText="true" GridPane.columnIndex="0" GridPane.columnSpan="2147483647" GridPane.rowIndex="0" GridPane.rowSpan="2">
<GridPane.margin>
<Insets bottom="5.0" />
</GridPane.margin>
</TextArea>
</children>
<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>
<padding>
<Insets left="5.0" right="5.0" />
</padding>
<rowConstraints>
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</fx:root>
更新
所以,我终于找到了问题所在。我有两个按钮&#39; fx:id在FXML中输入错误。
答案 0 :(得分:2)
null
中的某些按钮为initialize()
,因为FXML中没有相应fx:id
的元素。
为了注入这个:
@FXML private Button mcButton;
必须有<Button>
与相应的fx:id
:
<Button fx:id="mcButton" ... />
拨打电话,例如msButton.setText("XX");
(FXML中存在msButton
)。