Gui按钮不会单击javaFX

时间:2017-03-18 15:20:40

标签: java button javafx

我的项目是计算器。我已经给0按钮功能注册0到TextField,但它不起作用。有些聪明的人有解决方案吗?

public void start(Stage primaryStage) throws Exception{
    primaryStage.setTitle("This is a caculator");

    TextArea result = new TextArea("This is where the result will be"); result.setPrefSize(480,50); result.setLayoutX(10); result.setLayoutY(65);
    TextField textField = new TextField();  textField.setPrefSize(480,50); textField.setLayoutX(10);    textField.setLayoutY(10);

    Button button1 = new Button("1");       button1.setPrefSize(100,50);        button1.setTranslateX(30);      button1.setTranslateY(140);
    Button button2 = new Button("2");       button2.setPrefSize(100,50);        button2.setTranslateX(150);     button2.setTranslateY(140);
    Button button3 = new Button("3");       button3.setPrefSize(100,50);        button3.setTranslateX(270);     button3.setTranslateY(140);
    Button button4 = new Button("4");       button4.setPrefSize(100,50);        button4.setTranslateX(30);      button4.setTranslateY(210);
    Button button5 = new Button("5");       button5.setPrefSize(100,50);        button5.setTranslateX(150);     button5.setTranslateY(210);
    Button button6 = new Button("6");       button6.setPrefSize(100,50);        button6.setTranslateX(270);     button6.setTranslateY(210);
    Button button7 = new Button("7");       button7.setPrefSize(100,50);        button7.setTranslateX(30);      button7.setTranslateY(280);
    Button button8 = new Button("8");       button8.setPrefSize(100,50);        button8.setTranslateX(150);     button8.setTranslateY(280);
    Button button9 = new Button("9");       button9.setPrefSize(100,50);        button9.setTranslateX(270);     button9.setTranslateY(280);
    Button button0 = new Button("0");       button0.setPrefSize(100,50);        button0.setTranslateX(150);     button0.setTranslateY(350);
    Button buttonPlus = new Button("+");    buttonPlus.setPrefSize(100,50);     buttonPlus.setTranslateX(390);  buttonPlus.setTranslateY(210);
    Button buttonMinus = new Button("-");   buttonMinus.setPrefSize(100,50);    buttonMinus.setTranslateX(390); buttonMinus.setTranslateY(280);
    Button buttonMulti = new Button("*");   buttonMulti.setPrefSize(100,50);    buttonMulti.setTranslateX(390); buttonMulti.setTranslateY(350);
    Button buttonDivide = new Button("/");  buttonDivide.setPrefSize(100,50);   buttonDivide.setTranslateX(270);buttonDivide.setTranslateY(350);
    Button buttonClear = new Button("C");   buttonClear.setPrefSize(100,50);    buttonClear.setTranslateX(390); buttonClear.setTranslateY(140);
    Button buttonResult = new Button("=");  buttonResult.setPrefSize(100,50);   buttonResult.setTranslateX(30); buttonResult.setTranslateY(350);
    Button virus = new Button("Install the deadly virus"); virus.setPrefSize(450,50);   virus.setTranslateX(30);virus.setTranslateY(430);

    Pane pane = new Pane(textField,result,button1,button2,button3,buttonClear,button4,button5,button6,buttonPlus,button7,button8,button9,buttonMinus,buttonMulti,buttonDivide,button0,buttonResult,virus);

    button0.setOnAction(event -> textField.setText("0"));

    Scene scene = new Scene(pane,500,500);
    scene.getStylesheets().add("sample/css/Style.css");
    primaryStage.setScene(scene);
    primaryStage.show();
}

1 个答案:

答案 0 :(得分:0)

好吧,我会假设你想要追加它,连接它,把它添加到最后(所有相同的含义)

button0.setOnAction(e -> button0.appendText("0");
// // // OR // // //
button0.setOnAction(e -> button0.setText(button0.getText() + "0");

两者都会给出相同的结果,但我更喜欢第一个,更优雅。