JavaFx开发一个简单的计算器

时间:2024-03-27 16:45:40

最近比较无聊看了下JavaFx然后决定写一个简单的计算器,废多看蹦!

1、先下载一个JavaFX Scene Builder 2.0.exe  这个东西直接百度就下。

2、打开idea配置JavaFx如图

JavaFx开发一个简单的计算器

3、创建项目JavaFx

JavaFx开发一个简单的计算器

4、启动类

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.LinkedList;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("calculator.fxml"));
        primaryStage.setTitle("简单计算器");
        primaryStage.setScene(new Scene(root, 257, 361));
        primaryStage.show();

    }


    public static void main(String[] args) {
      launch(args);


    }
}

5、计算器控制器

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.math.BigDecimal;
import java.net.URL;
import java.util.LinkedList;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

public class Calculator implements Initializable {


    @FXML
    private TextField result;

    private LinkedList<String> numbers=new LinkedList();

    private LinkedList<String> yunsuanfu=new LinkedList<>();

    private BigDecimal bigDecimal;
    Pattern pattern=Pattern.compile("\\d+|\\d.?\\d");

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }


    public void pressNumberButton(ActionEvent event){
        String number = getButtonValue(event);
        String textResult = result.getText();
        if(".".equals(number.trim())){
            boolean contains = textResult.contains(".");
            if(contains||isBlank(textResult)){
                return;
            }else {
                result.setText(textResult+".");
                return;
            }
        }
        result.setText(result.getText()+number);
    }


    public void pressYunsuanfu(ActionEvent event){
        String text = result.getText();

        if (!pattern.matcher(text).matches()) {
            return;
        }
        if (isBlank(text)){
            return;
        }
        numbers.add(text);
        String buttonValue = getButtonValue(event);
        yunsuanfu.add(buttonValue);
        result.setText("");
    }



    public void pressDengyu(ActionEvent event) {
        boolean numbersEmpty = numbers.isEmpty();
        boolean yunsuanfuEmpty = yunsuanfu.isEmpty();
        if(numbersEmpty||yunsuanfuEmpty){
            result.setText(result.getText());
            return;
        }

        if(numbers.size()>0){
            numbers.add(result.getText());
            bigDecimal=new BigDecimal(numbers.getFirst());
            for (int i=1;i<numbers.size();i++){
                switch (yunsuanfu.get(i-1)){
                    case "+":
                        bigDecimal=bigDecimal.add(new BigDecimal(numbers.get(i)));
                        break;
                    case "-":
                        bigDecimal=bigDecimal.subtract(new BigDecimal(numbers.get(i)));
                        break;
                    case "/":
                        BigDecimal decimal = new BigDecimal(numbers.get(i));
                        int compareTo = decimal.compareTo(new BigDecimal("0"));
                        if(compareTo==0){
                            result.setText("除数不能为0");
                            numbers.clear();
                            yunsuanfu.clear();
                            return;
                        }
                        this.bigDecimal = bigDecimal.divide(new BigDecimal(numbers.get(i)), 20, BigDecimal.ROUND_UP);
                        break;
                    case "*":
                        this.bigDecimal = this.bigDecimal.multiply(new BigDecimal(numbers.get(i)));
                        break;

                }
            }
            result.setText(bigDecimal.stripTrailingZeros().toString());
            numbers.clear();
            yunsuanfu.clear();
        }
    }


    public void pressClear(ActionEvent event) {
        result.setText("");
        yunsuanfu.clear();
        numbers.clear();
    }

    public void pressDeleteOne(ActionEvent event) {
        String text = result.getText();
        if(text!=null&&text!=""&&text.length()>0){
            text=text.substring(0,text.length()-1);
            result.setText(text);
        }
    }


    private String getButtonValue(ActionEvent event){
        Button button = (Button)event.getSource();
        return button.getText();
    }

    private boolean isBlank(String text){
        if(text!=null&&text!=""&&text.length()>0){
            return false;
        }
        return true;
    }

}

6、计算器页面布局,新建一个FXML的文件,然后右键用JavaFX Scene Builder 2.0.exe这个打开编辑,直接拖动组件就行。

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="361.0" prefWidth="257.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Calculator">
   <children>
      <TextField fx:id="result" layoutX="29.0" layoutY="23.0" prefHeight="31.0" prefWidth="202.0" />
      <Button fx:id="seven" layoutX="29.0" layoutY="129.0" mnemonicParsing="false" onAction="#pressNumberButton" text="7" />
      <Button fx:id="eight" layoutX="86.0" layoutY="128.0" mnemonicParsing="false" onAction="#pressNumberButton" text="8" />
      <Button fx:id="nine" layoutX="143.0" layoutY="129.0" mnemonicParsing="false" onAction="#pressNumberButton" text="9" />
      <Button fx:id="cheng" layoutX="197.0" layoutY="128.0" mnemonicParsing="false" onAction="#pressYunsuanfu" prefHeight="31.0" prefWidth="31.0" text="*" />
      <Button fx:id="four" layoutX="29.0" layoutY="188.0" mnemonicParsing="false" onAction="#pressNumberButton" text="4" />
      <Button fx:id="five" layoutX="86.0" layoutY="188.0" mnemonicParsing="false" onAction="#pressNumberButton" text="5" />
      <Button fx:id="six" layoutX="143.0" layoutY="188.0" mnemonicParsing="false" onAction="#pressNumberButton" text="6" />
      <Button fx:id="jian" layoutX="197.0" layoutY="188.0" mnemonicParsing="false" onAction="#pressYunsuanfu" prefHeight="31.0" prefWidth="31.0" text="-" />
      <Button fx:id="one" layoutX="29.0" layoutY="247.0" mnemonicParsing="false" onAction="#pressNumberButton" prefHeight="31.0" prefWidth="29.0" text="1" />
      <Button fx:id="two" layoutX="86.0" layoutY="247.0" mnemonicParsing="false" onAction="#pressNumberButton" text="2" />
      <Button fx:id="three" layoutX="143.0" layoutY="247.0" mnemonicParsing="false" onAction="#pressNumberButton" text="3" />
      <Button fx:id="jia" layoutX="197.0" layoutY="247.0" mnemonicParsing="false" onAction="#pressYunsuanfu" text="+" />
      <Button fx:id="dian" layoutX="29.0" layoutY="301.0" mnemonicParsing="false" onAction="#pressNumberButton" prefHeight="31.0" prefWidth="29.0" text="." />
      <Button fx:id="zero" layoutX="86.0" layoutY="301.0" mnemonicParsing="false" onAction="#pressNumberButton" text="0" />
      <Button fx:id="dengyu" layoutX="142.0" layoutY="301.0" mnemonicParsing="false" onAction="#pressDengyu" prefHeight="31.0" prefWidth="31.0" text="=" />
      <Button fx:id="chu" layoutX="197.0" layoutY="301.0" mnemonicParsing="false" onAction="#pressYunsuanfu" prefHeight="31.0" prefWidth="31.0" text="/" />
      <Button fx:id="clear" layoutX="29.0" layoutY="77.0" mnemonicParsing="false" onAction="#pressClear" prefHeight="31.0" prefWidth="76.0" text="clear" />
      <Button fx:id="deleteone" layoutX="152.0" layoutY="77.0" mnemonicParsing="false" onAction="#pressDeleteOne" prefHeight="31.0" prefWidth="76.0" text="←" />
   </children>
</AnchorPane>

7、然后运行

JavaFx开发一个简单的计算器