我收到错误“ 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);
}
}
预先感谢
答案 0 :(得分:0)
我猜这是发生的行(检查堆栈跟踪):HistoWindowController histoControl = loader.getController();
HistoryView.fxml中fx:controller属性的值是MainWindowController
感谢@fabian