繁体   English   中英

使用JAVAFX时出现java.lang.ClassCastException

[英]java.lang.ClassCastException whilst using JAVAFX

我收到错误“ java.lang.ClassCastException”。 我正在尝试制作一个计算器,并使用JAVAFX(如果需要,可以使用SceneBuilder),并且我无法跟踪我在代码上哪里做错了。

我试图在单击“历史记录”按钮时显示一个新窗口,并且该窗口应显示之前执行的所有操作。

java.lang.ClassCastException:无法将application.controller.MainWindowController强制转换为application.controller.HistoWindowController

MainWindowController是:

public class MainWindowController {

private Main main;

@FXML
TextField input1;
@FXML
TextField input2; 
@FXML
Label showAnswer;

public void setMain(Main main){
    this.main = main;
}
public void showAnswerSTR(String str) {
    showAnswer.setText("Answer:  " + str);;
}
@FXML
public void showHistory() {
    main.HistoryViewer();
}
@FXML
public void addNumbers(){
    Float inputA = Float.parseFloat(input1.getText());
    Float inputB = Float.parseFloat(input2.getText());
    Addition x = new Addition();
    String ans = x.operation(inputA, inputB);
    showAnswerSTR(ans);
}
@FXML
public void subtractNumbers(){
    Float inputA = Float.parseFloat(input1.getText());
    Float inputB = Float.parseFloat(input2.getText());
    Subtraction x = new Subtraction();
    String ans = x.operation(inputA, inputB);
    showAnswerSTR(ans);
}
@FXML
public void multiplyNumbers(){
    Float inputA = Float.parseFloat(input1.getText());
    Float inputB = Float.parseFloat(input2.getText());
    Multiplication x = new Multiplication();
    String ans = x.operation(inputA, inputB);
    showAnswerSTR(ans);
}
@FXML
public void divideNumbers(){
    Float inputA = Float.parseFloat(input1.getText());
    Float inputB = Float.parseFloat(input2.getText());
    Division x = new Division();
    String ans = x.operation(inputA, inputB);
    showAnswerSTR(ans);
}
}

HistoWindowController是:

public class HistoWindowController {

@FXML
VBox HistoryViewer;

public void showHistory(){
    StringTokenizer str = new StringTokenizer(getHistory(),";");
    while(str.hasMoreTokens()){
        HistoryViewer.getChildren().add(new Label(str.nextToken().toString()));
    }
}

private String history = "H I S T O R Y;";

public void addHistory(String history) {
    this.history += history + ";";
}

public String getHistory() {
    return history;
}

/*public String historyReader() {

    StringTokenizer str = new StringTokenizer(getHistory(),";");
    String temp = "";
        if(str.hasMoreTokens()) {
            temp += str.nextToken();
            temp += "\n";
        }
    return temp;
}*/
}

主要是:

public class Main extends Application {

private Stage primaryStage;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/mainView.fxml"));
        AnchorPane mainFXML = (AnchorPane) loader.load();

        Scene scene = new Scene(mainFXML);
        primaryStage.setScene(scene);
        primaryStage.show();

        MainWindowController mainWindow = loader.getController();
        mainWindow.setMain(this);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void HistoryViewer(){
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/HistoryView.fxml"));
        AnchorPane histoView = (AnchorPane) loader.load();

        Scene scene = new Scene(histoView);
        primaryStage.setScene(scene);
        primaryStage.show();


        HistoWindowController histoControl = loader.getController();
        histoControl.showHistory();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
}

提前致谢

我猜这是发生的行(检查stacktrace):HistoWindowController histoControl = loader.getController();

HistoryView.fxml中fx:controller属性的值是MainWindowController

谢谢@fabian

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM