0
我正在創建一個簡單的計算器,它將在文本字段中輸入的兩個值相加,然後在按下按鈕時將總和顯示在第三個文本字段中。然而,當我按下按鈕什麼都沒有發生,我不知道什麼是錯的。我沒有收到任何錯誤消息,程序編譯按鈕只是沒有做任何事情。JavaFX按鈕監聽器無法正常工作
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
public class Calculator extends Application{
private TextField sumField;
private TextField firstVField;
private TextField secondVField;
public void start(Stage myStage){
myStage.setTitle("Simple Calculator");
GridPane rootNode = new GridPane();
rootNode.setPadding(new Insets(30));
rootNode.setHgap(5);
rootNode.setVgap(5);
Scene myScene = new Scene(rootNode, 350,250);
Label firstVLabel = new Label("First Value:");
Label secondVLabel = new Label("Second Value:");
Label sumLabel = new Label("Sum:");
TextField firstVField = new TextField();
TextField secondVField = new TextField();
TextField sumField = new TextField();
sumField.setEditable(false);
Button calculate = new Button("Calculate");
rootNode.add(firstVLabel, 0, 0);
rootNode.add(firstVField, 1, 0);
rootNode.add(secondVLabel, 0, 1);
rootNode.add(secondVField, 1, 1);
rootNode.add(sumLabel, 0, 2);
rootNode.add(sumField, 1, 2);
rootNode.add(calculate, 1, 3);
myStage.setScene(myScene);
myStage.setResizable(false);
myStage.show();
}
class ButtonHandler implements EventHandler<ActionEvent>{
public void handle(ActionEvent e) {
int sum = (int)firstVField.getText() + (int)secondVField.getText();
sumField.setText(Integer.toString(sum));
}
}
public static void main(String [] args){
launch(args);
}
}
添加行我仍然收到此錯誤信息http://imgur.com/VJj43Ju – grademacher
那是一個非常不同的問題後。請參閱[本文](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) – Reimeus
將轉換參數更改爲parseInt參數和我現在得到一個空指針異常。有什麼想法嗎? – grademacher