JavaFx-编写一个简单的计算器,实现加减乘除

2年前 (2022) 程序员胖胖胖虎阿
183 0 0

0.题目描述

编写一个简单的计算器,完成加、减、乘、除的功能

JavaFx-编写一个简单的计算器,实现加减乘除

 

 

1.源代码

 

 

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

public class Test2 extends Application {
    private TextField num1 = new TextField();
    private TextField num2 = new TextField();
    private TextField res = new TextField();
    
    private Button btAdd = new Button("Add");
    private Button btSub = new Button("Subtract");
    private Button btMul = new Button("Multiply");
    private Button btDiv = new Button("Divide");
    
    @Override
    public void start(Stage primaryStage) {
        HBox top = new HBox();
        top.setAlignment(Pos.CENTER);
        top.setPadding(new Insets(5,10,5,10));
        top.setSpacing(10);
        
        top.getChildren().addAll(
            new Label("Number1"),
            num1,
            new Label("Number2"),
            num2,
            new Label("Result"),
            res);
        
        HBox bottom = new HBox();
        bottom.setAlignment(Pos.CENTER);
        bottom.setPadding(new Insets(5,10,5,10));
        bottom.setSpacing(10);
        
        bottom.getChildren().addAll(btAdd,btSub,btMul,btDiv);
            
        BorderPane pane = new BorderPane();
        pane.setTop(top);
        pane.setBottom(bottom);    
        
        btAdd.setOnAction(e -> Add());
        btSub.setOnAction(e -> Sub());
        btMul.setOnAction(e -> Mul());
        btDiv.setOnAction(e -> Div());
        
        Scene scene = new Scene(pane, 900, 80);
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Calculator");
        primaryStage.show();
        
    }
    
    private void Add() {
        double n1 = Double.parseDouble(num1.getText());
        double n2 = Double.parseDouble(num2.getText());
        res.setText(String.format("%.2f", n1+n2));
    }
    
    private void Sub() {
        double n1 = Double.parseDouble(num1.getText());
        double n2 = Double.parseDouble(num2.getText());
        res.setText(String.format("%.2f", n1-n2));
    }
    
    private void Mul() {
        double n1 = Double.parseDouble(num1.getText());
        double n2 = Double.parseDouble(num2.getText());
        res.setText(String.format("%.2f", n1*n2));
    }
    
    private void Div() {
        double n1 = Double.parseDouble(num1.getText());
        double n2 = Double.parseDouble(num2.getText());
        res.setText(String.format("%.2f", n1/n2));
    }
    
    public static void main(String[] args) {
        Application.launch(args);
    }

}

 

2.运行结果

JavaFx-编写一个简单的计算器,实现加减乘除

 

 3.技术总结

  上半部分放入一个HBox中,下半部分放入一个HBox中。在用一个BorderPane装这两个HBox。

  难点在于如何获取TextField中的数值,使用 Double.parseDouble(name.getText()) 将TextField对象name中的文本转换成double类型数值,name.setText(String.format("%.2f", num)) 设置数值。

 

 

 

 

 

相关文章

暂无评论

暂无评论...