2 q1242027878 q1242027878 于 2016.04.10 10:13 提问

关于JavaFX中按钮事件

import javafx.application.*;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.*;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class Calculate extends Application{

public void start(Stage primaryStage)
{
    TextField tfNum1 = new TextField();
    tfNum1.setMaxWidth(50);
    TextField tfNum2 = new TextField();
    tfNum2.setMaxWidth(50);
    TextField tfResult = new TextField();
    tfResult.setMaxWidth(50);

    Text num1 = new Text("Number1:");
    Text num2 = new Text("Number2:");
    Text result = new Text("Result:");

    Button btAdd = new Button("Add");
    Button btSubtract = new Button("Subtract");
    Button btMultiply = new Button("Multiply");
    Button btDivide = new Button("Divide");


    GridPane gridPane = new GridPane();
    gridPane.add(num1, 0, 0);
    gridPane.add(tfNum1, 1, 0);
    gridPane.add(num2, 2, 0);
    gridPane.add(tfNum2, 3, 0);
    gridPane.add(result, 4, 0);
    gridPane.add(tfResult, 5,0);
    FlowPane pane = new FlowPane();
    pane.getChildren().addAll(btAdd,btSubtract,btMultiply,btDivide);
    pane.setHgap(5);
    pane.setAlignment(Pos.CENTER);
    BorderPane borderPane = new BorderPane();
    borderPane.setTop(gridPane);
    borderPane.setCenter(pane);


    btAdd.setOnAction(oa->{
        double n1 = Double.parseDouble(tfNum1.getText());
        double n2 = Double.parseDouble(tfNum2.getText());
        double  r =n1+n2;
        tfResult.setText(String.valueOf(r));
    });
    btSubtract.setOnAction(oa->{
        double n1 = Double.parseDouble(tfNum1.getText());
        double n2 = Double.parseDouble(tfNum2.getText());
        double  r =n1-n2;
        tfResult.setText(String.valueOf(r));
    });
    btMultiply.setOnAction(oa->{
        double n1 = Double.parseDouble(tfNum1.getText());
        double n2 = Double.parseDouble(tfNum2.getText());
        double  r =n1*n2;
        tfResult.setText(String.valueOf(r));
    });
    btDivide.setOnAction(oa->{
        double n1 = Double.parseDouble(tfNum1.getText());
        double n2 = Double.parseDouble(tfNum2.getText());
        if(n2==0)
        {
            Button btOK = new Button("OK");
            Text text1 = new Text("除数不能为0");
            GridPane pane2 = new GridPane();
            pane2.add(text1, 0, 0);
            pane2.add(btOK, 1, 1);

            Scene scene2 = new Scene(pane2,150,40);
            Stage prompt = new Stage();
            prompt.setScene(scene2);
            prompt.setTitle("error");
            prompt.show();

            btOK.setOnAction(e->{
                prompt.close();
            });
        }
        else{
        double  r =n1/n2;
        tfResult.setText(String.valueOf(r));}
    });

    Scene scene = new Scene(borderPane,320,50);
    primaryStage.setTitle("Calculator");
    primaryStage.setScene(scene);
    primaryStage.show();

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Application.launch(args);
}

}

以上是我的代码,我想问可不可以设置一个方法,判断是哪个按钮被按下呢?除了用鼠标的坐标判断。

1个回答

CSDNXIAON
CSDNXIAON   2016.04.10 10:22

JAVAFX-事件
jqgrid 按钮事件
JAVAFX ComboBox的选中事件
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!