实现目标
如上,实现一个这样的计算器的功能
代码结构
全部代码
一个Main类
package ComputerTest;
import java.util.LinkedList;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
private String logic;//存运算符的变量 null
private TextField view;
private String out="";
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(420);
primaryStage.setResizable(false);
addComp(root);
primaryStage.getIcons().add(new Image("ComputerTest/computer.png", 256, 256, true, true));
primaryStage.setTitle("Calculator");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
//添加组件的方法
private void addComp(AnchorPane root) {
//添加显示框
view = new TextField("0");
view.setMinSize(280, 50);
view.setLayoutX(10);
view.setLayoutY(15);
//设置只读
view.setEditable(false);
//内容右对齐
view.setAlignment(Pos.CENTER_RIGHT);
//设置字体
view.setFont(new Font("华文琥珀", 20));
root.getChildren().add(view);
//添加一个表格布局
GridPane gridPane = new GridPane();
//设置布局中元素组件横向间距
gridPane.setHgap(1);
//设置布局中元素组件纵向间距
gridPane.setVgap(1);
gridPane.setLayoutY(65);
gridPane.setPrefWidth(290);
gridPane.setPrefHeight(300);
gridPane.setPadding(new Insets(10));
//列容器
root.getChildren().add(gridPane);
//创建数字监听器对象
NumberEvent numberEvent = new NumberEvent();
//添加按钮
Button btn_7 = new MyButton("7");
btn_7.setOnMouseClicked(numberEvent);
gridPane.add(btn_7,0,1);
Button btn_8 = new MyButton("8");
btn_8.setOnMouseClicked(numberEvent);
gridPane.add(btn_8,1,1);
Button btn_9 = new MyButton("9");
btn_9.setOnMouseClicked(numberEvent);
gridPane.add(btn_9,2,1);
Button btn_4 = new MyButton("4");
btn_4.setOnMouseClicked(numberEvent);
gridPane.add(btn_4,0,2);
Button btn_5 = new MyButton("5");
btn_5.setOnMouseClicked(numberEvent);
gridPane.add(btn_5,1,2);
Button btn_6 = new MyButton("6");
btn_6.setOnMouseClicked(numberEvent);
gridPane.add(btn_6,2,2);
Button btn_1 = new MyButton("1");
btn_1.setOnMouseClicked(numberEvent);
gridPane.add(btn_1,0,3);
Button btn_2 = new MyButton("2");
btn_2.setOnMouseClicked(numberEvent);
gridPane.add(btn_2,1,3);
Button btn_3 = new MyButton("3");
btn_3.setOnMouseClicked(numberEvent);
gridPane.add(btn_3,2,3);
Button btn_0 = new MyButton("0");
btn_0.setOnMouseClicked(numberEvent);
gridPane.add(btn_0,0,4,2,1);
Button btn_C = new MyButton("C");//鼠标点击到置零的操作
btn_C.setOnMouseClicked(new EventHandler<Event>() {
public void handle(Event event) {
out="";
view.setText("0");
}
});
gridPane.add(btn_C,0,0,2,1);
Button btn_eq = new MyButton("=");
btn_eq.setStyle("-fx-background-color: blue");
btn_eq.setOnMouseClicked(new EventHandler<Event>() {
public void handle(Event event) {
dispose();
}
});
gridPane.add(btn_eq,3,4);
//创建监听器对象
LogicEvent logicEvent = new LogicEvent();
Button btn_add = new MyButton("+");
btn_add.setStyle("-fx-background-color: blue");
btn_add.setOnMouseClicked(logicEvent);
gridPane.add(btn_add,3,3);
Button btn_sub = new MyButton("-");
btn_sub.setStyle("-fx-background-color: blue");
btn_sub.setOnMouseClicked(logicEvent);
gridPane.add(btn_sub,3,2);
Button btn_mul = new MyButton("x");
btn_mul.setStyle("-fx-background-color: blue");
btn_mul.setOnMouseClicked(logicEvent);
gridPane.add(btn_mul,3,1);
Button btn_div = new MyButton("/");
btn_div.setStyle("-fx-background-color: blue");
btn_div.setOnMouseClicked(logicEvent);
gridPane.add(btn_div,3,0);
Button btn_Point=new MyButton(".");
btn_Point.setOnMouseClicked(logicEvent);
gridPane.add(btn_Point, 2,4);
Button btn_X=new MyButton("<=]");
btn_X.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(out.length()>0){
out=""+out.substring(0,out.length()-1);
}
view.setText(out);
}
});
btn_X.setFont(new Font("华文琥珀",20));//重写
gridPane.add(btn_X, 2,0);
}
//数字监听器
class NumberEvent implements EventHandler<Event>{
public void handle(Event event) {
//获取事件源 (获取触发事件的按钮)
MyButton btn = (MyButton)event.getSource();
//先获取当前按钮的内容
String btnNumber = btn.getText();
out+=btnNumber;
view.setText(out);
}
}
//运算符的事件监听器
class LogicEvent implements EventHandler<Event>{
public void handle(Event event) {
//得到按钮
MyButton logicBtn = (MyButton)event.getSource();
//获取按钮内容
logic = logicBtn.getText();
out+=logic;
view.setText(out);
}
}
//对文本框中的东西进行处理
private void dispose(){
LinkedList<String> list=new LinkedList<>();
int t=0;
for (int i = 0; i < out.length(); i++) {
char c=out.charAt(i);
if (c=='+'||c=='-'||c=='x'||c=='/') {
String temp=out.substring(t,i);
list.add(temp);
list.add(out.substring(i,i+1));
t=i+1;
}
}
list.add(out.substring(t,out.length()));
//先处理掉所有的乘除运算
while(true){
t=list.size();
for (int i = 0; i < list.size(); i++) {
String st=list.get(i);
if (st.equals("x")||st.equals("/")) {
double temp=Double.parseDouble(list.get(i-1));
if (st.equals("x")) {
temp*=Double.parseDouble(list.get(i+1));
}else {
temp/=Double.parseDouble(list.get(i+1));
}
String string=""+temp;
list.set(i-1, string);
list.remove(i);
list.remove(i);
}
}
if (t==list.size()) {//如果集合的长度已经不发生改变了
break;//则说明找完了
}
}
//最后 处理加减运算
while(list.size()!=1){
int i=0;
for (i = 0; i < list.size(); i++) {
String st=list.get(i);
if (st.equals("+")||st.equals("-")) {
double temp=Double.parseDouble(list.get(i-1));
if (st.equals("+")) {
temp+=Double.parseDouble(list.get(i+1));
}else {
temp-=Double.parseDouble(list.get(i+1));
}
String string=""+temp;
list.set(i-1, string);
list.remove(i);
list.remove(i);
i=0;//特别注意加减是要按照顺序算的!!!一个坑
//没有这个,就会出现1-2-3-4的计算(1-2)-(3-4)的效果!!
}
}
}
view.setText(list.get(0));
}
public static void main(String[] args) {
launch(args);
}
}
一个MyButton类
package ComputerTest;
import javafx.scene.control.Button;
import javafx.scene.text.Font;
public class MyButton extends Button{
public MyButton(String text) {
super(text);
setPrefWidth(160);
setPrefHeight(150);
setFont(new Font("华文琥珀",30));
}
}
测试效果
计算结果
核心算法(用于测试的)
package Test1;
import java.util.LinkedList;
public class test {
public static void main(String[] args) {
String out="1.2x2+5-45-7.2/3";
LinkedList<String> list=new LinkedList<>();
int t=0;
for (int i = 0; i < out.length(); i++) {
char c=out.charAt(i);
if (c=='+'||c=='-'||c=='x'||c=='/') {
String temp=out.substring(t,i);
list.add(temp);
list.add(out.substring(i,i+1));
t=i+1;
}
}
list.add(out.substring(t,out.length()));
System.out.println(list);
//先处理掉所有的乘除运算
while(true){
t=list.size();
for (int i = 0; i < list.size(); i++) {
String st=list.get(i);
if (st.equals("x")||st.equals("/")) {
double temp=Double.parseDouble(list.get(i-1));
if (st.equals("x")) {
temp*=Double.parseDouble(list.get(i+1));
}else {
temp/=Double.parseDouble(list.get(i+1));
}
String string=""+temp;
list.set(i-1, string);
list.remove(i);
list.remove(i);
}
}
if (t==list.size()) {//如果集合的长度已经不发生改变了
break;//则说明找完了
}
}
System.out.println(list);
//最后 处理加减运算
while(list.size()!=1){
int i=0;
for (i = 0; i < list.size(); i++) {
String st=list.get(i);
if (st.equals("+")||st.equals("-")) {
double temp=Double.parseDouble(list.get(i-1));
if (st.equals("+")) {
temp+=Double.parseDouble(list.get(i+1));
}else {
temp-=Double.parseDouble(list.get(i+1));
}
String string=""+temp;
list.set(i-1, string);
list.remove(i);
list.remove(i);
i=0;//特别注意加减是要按照顺序算的!!!一个坑
//没有这个,就会出现1-2-3-4的计算(1-2)-(3-4)的效果!!
}
}
}
System.out.println(list);
}
}
这个核心算法主要是对我们计算器输入的字符串进行处理,上面的out是可以自定义的,在计算器的代码的实现中,dispose函数的作用就是把这个算法的放进去的,测试的时候,推荐更改不同的out的值,进行测试,出现问题了,推荐加上一些输出语句看效果,当然,会debug的,用debug更好了。
上面把这个test的测试的代码的结果截图来看一看。
百度网盘提取
链接:https://pan.baidu.com/s/1icZ4f0KvnPJz02VmJTN7ig
提取码:je3v
复制这段内容后打开百度网盘手机App,操作更方便哦
版权声明:本文为qq_43918130原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。