用java写计算器
你好!这是本人用java写的一款计算器,因为是初学,所以代码结构很乱不规则,且会出现很多变量类型或语句不严谨等等问题,希望谅解。
计算器样式是仿照这个博主的计算器设计的,计算器计算算法是本人自己编织。
添加链接描述
https://blog.csdn.net/qq_41915690/article/details/80464010
package test2;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class reJButton extends JButton{
double i = 0;
reJButton(String text){
super(text);
}
}
class Calc extends JFrame implements ActionListener{
int wei_shu = 0;
int index = 0;
int point = 0;
boolean jia = false;
boolean jian = false;
boolean cheng = false;
boolean chu = false;
boolean jia_cheng = false;
boolean jian_cheng = false;
boolean jia_chu = false;
boolean jian_chu = false;
boolean deng = false;
int count_jia = 0;
int count_jian = 0;
int count_cheng = 0;
int count_chu = 0;
double num_1 = 0.0;
double num_2 = 0.0;
double sum_1 = 0.0;
double last_2 = 0.0;
double sum_2 = 0.0;
double more_suan = 0;
double temp = 0;
double temp2 = 0.0;
int jia_mark = 0;
int jian_mark = 0;
int cheng_mark = 0;
int chu_mark = 0;
String last_yun_suan = "";
String ss;
String ss2 = "";
reJButton b_num0 = new reJButton("0");
reJButton b_num1 = new reJButton("1");
reJButton b_num2 = new reJButton("2");
reJButton b_num3 = new reJButton("3");
reJButton b_num4 = new reJButton("4");
reJButton b_num5 = new reJButton("5");
reJButton b_num6 = new reJButton("6");
reJButton b_num7 = new reJButton("7");
reJButton b_num8 = new reJButton("8");
reJButton b_num9 = new reJButton("9");
reJButton b_jia = new reJButton("+");
reJButton b_jian = new reJButton("-");
reJButton b_cheng = new reJButton("×");
reJButton b_chu = new reJButton("÷");
reJButton b_equal = new reJButton("=");
reJButton b_point = new reJButton(".");
reJButton b_clear = new reJButton("C");
reJButton b_tuiwei = new reJButton("<--");
JPanel panel = new JPanel();
JTextField txt2 = new JTextField();
Calc(){
setResizable(false);
setLocation(500, 120);
setSize(390,500);
setVisible(true);
setTitle("计算器");
setDefaultCloseOperation(EXIT_ON_CLOSE);
txt2.setText("");
setTextField();
setButton();
setPanel(panel);
}
void setPanel(JPanel panel) {
this.getContentPane().add(panel);
panel.setLayout(null);
panel.add(b_num0);
panel.add(b_num1);
panel.add(b_num2);
panel.add(b_num3);
panel.add(b_num4);
panel.add(b_num5);
panel.add(b_num6);
panel.add(b_num7);
panel.add(b_num8);
panel.add(b_num9);
panel.add(b_jia);
panel.add(b_jian);
panel.add(b_cheng);
panel.add(b_chu);
panel.add(b_equal);
panel.add(b_point);
panel.add(b_clear);
panel.add(b_tuiwei);
panel.add(txt2);
}
void setTextField() {
txt2.setLocation(20, 10);
txt2.setSize(335, 80);
txt2.setFont(new Font("宋体", Font.PLAIN, 50));
txt2.setBorder(BorderFactory.createLoweredBevelBorder());
txt2.setBackground(Color.lightGray);
txt2.setEditable(false);
}
void setButton() {
init_Button(b_num0,20,387,125,43);//0
init_Button(b_equal,300,315,55,115);//=
init_Button(b_num7,20,171,55,43);
init_Button(b_num4,20,243,55,43);
init_Button(b_num1,20,315,55,43);
init_Button(b_num8,90,171,55,43);
init_Button(b_num5,90,243,55,43);
init_Button(b_num2,90,315,55,43);
init_Button(b_num9,160,171,55,43);//160
init_Button(b_num6,160,243,55,43);
init_Button(b_num3,160,315,55,43);
init_Button(b_point,160,387,55,43);//.
init_Button(b_jia,230,387,55,43);//+
init_Button(b_jian,230,315,55,43);//-
init_Button(b_cheng,230,243,55,43);//cheng
init_Button(b_chu,230,171,55,43);// chu
init_Button(b_clear,300,243,55,43);//C
init_Button(b_tuiwei,300,171,55,43);//<--
b_num0.i = 0;
b_num1.i = 1;
b_num2.i = 2;
b_num3.i = 3;
b_num4.i = 4;
b_num5.i = 5;
b_num6.i = 6;
b_num7.i = 7;
b_num8.i = 8;
b_num9.i = 9;
b_num0.addActionListener(this);
b_num1.addActionListener(this);
b_num2.addActionListener(this);
b_num3.addActionListener(this);
b_num4.addActionListener(this);
b_num5.addActionListener(this);
b_num6.addActionListener(this);
b_num7.addActionListener(this);
b_num8.addActionListener(this);
b_num9.addActionListener(this);
b_jia.addActionListener(this);
b_jian.addActionListener(this);
b_cheng.addActionListener(this);
b_chu.addActionListener(this);
b_equal.addActionListener(this);
b_point.addActionListener(this);
b_clear.addActionListener(this);
b_tuiwei.addActionListener(this);
}
void init_Button(reJButton b, int x , int y , int w , int h) {
b.setLocation(x, y);
b.setSize(w, h);
b.setFont(new Font("标楷体",Font.BOLD,15));
b.setBackground(Color.ORANGE);
b.setForeground(Color.BLACK);
b.setBorder(BorderFactory.createRaisedBevelBorder());
}
void reset() {
point = 0;
index = 0;
sum_1 = 0;
wei_shu = 0;
jian_mark = 0;
cheng_mark = 0;
jia_mark = 0;
chu_mark = 0;
count_jian = 0;
count_jia = 0;
count_cheng = 0;
count_chu = 0;
}
void show_twice(String s) {
if(deng) {
ss2 = String.valueOf(more_suan);
ss2 = ss2 + s;
txt2.setText(ss2);
}
}
/******************************************************************************/
double jian_fa(double num3, double num4) {
double sum = num3 - num4;
return sum;
}
double jia_fa(double num3, double num4) {
double sum = num3 + num4;
return sum;
}
double cheng_fa(double num3, double num4) {
double sum = num3 * num4;
return sum;
}
double chu_fa(double num3, double num4) {
double sum = num3 / num4;
return sum;
}
/*************************************************************************************/
void set_show(String s) {
switch(s) {
case "C":
ss2 = "";
break;/***************************************************************************************/
case "<--":
if(ss2.length() > 1) {
ss2 = ss2.substring(0, ss2.length()-1);
txt2.setText(ss2);}
else {
ss2 = "0";
txt2.setText(ss2);
ss2 = "";}
break;/**********************************************************************************************/
case "+":
if(count_jia == 0) {
ss2 = ss2 + "+";
txt2.setText(ss2);
}
else {
txt2.setText(ss2);
}
show_twice(s);
break;/***************************************************************************************/
case "-":
if(count_jian == 0) {
ss2 = ss2 + "-";
txt2.setText(ss2);
}
else {
txt2.setText(ss2);
}
show_twice(s);
break;/**********************************************************************************/
case "×":
if(count_cheng == 0) {
ss2 = ss2 + "×";
txt2.setText(ss2);
}
else {
txt2.setText(ss2);
}
show_twice(s);
break;/******************************************************************/
case "÷":
if(count_chu == 0) {
ss2 = ss2 + "÷";
txt2.setText(ss2);
}
else {
txt2.setText(ss2);
}
show_twice(s);
break;/********************************************************************/
default:
ss2 = ss2 + s;
txt2.setText(ss2);
}
}
void yun_suan_more_jia_or_jian() {
if(deng) {
num_1 = more_suan;
deng = false;
}
}
void yun_suan_more_cheng_or_chu() {
if(deng) {
num_2 = more_suan;
deng = false;
}
}
void suanfa_jia() {
if(jia){
yun_suan_more_jia_or_jian();
if(jian_mark != 0) {
sum_1 = jian_fa(sum_1, num_1);}
else if(cheng_mark != 0) {
sum_1 = cheng_fa(sum_1, num_2);}
else if(chu_mark != 0) {
sum_1 = chu_fa(sum_1, num_2);}
else
sum_1 = jia_fa(sum_1, num_1);
num_1 = 0;
num_2 = 1;
cheng_mark = 0;
jian_mark = 0;
chu_mark = 0;
jia_mark ++;
jia = false;
}
}
void suanfa_jian() {
if(jian) {
yun_suan_more_jia_or_jian();
if(cheng_mark != 0) {
sum_1 = cheng_fa(sum_1, num_2);}
else if(chu_mark != 0) {
sum_1 = chu_fa(sum_1, num_2);}
else if(jian_mark == 0)
sum_1 = jia_fa(sum_1, num_1);
else
sum_1 = jian_fa(sum_1, num_1);
num_1 = 0;
num_2 = 1;
cheng_mark = 0;
jia_mark = 0;
chu_mark = 0;
jian_mark ++;
jian = false;
}
}
void suanfa_cheng() {
if(cheng) {
yun_suan_more_cheng_or_chu();
if(jian_mark != 0) {
jian_cheng = true;
if(cheng_mark == 0) {
sum_2 = num_2;
num_2 = 1;
}
sum_2 = cheng_fa(sum_2, num_2);
}
else if(jia_mark != 0) {
jia_cheng = true;
if(cheng_mark == 0) {
sum_2 = num_2;
num_2 = 1;
}
sum_2 = cheng_fa(sum_2, num_2);
}
else if(chu_mark != 0) {
sum_1 = chu_fa(sum_1, num_2);
num_2 = sum_1;
}
else if(cheng_mark == 0) {
sum_1 = 1;
sum_1 = cheng_fa(sum_1, num_2);
num_2 = 1;
}
else {
sum_1 = cheng_fa(sum_1, num_2);
num_2 = 1.0;
}
num_1 = 0;
num_2 = 1;
jian_mark = 0;
cheng_mark++;
chu_mark = 0;
jia_mark = 0;
cheng = false;
}
}
void suanfa_chu() {
if(chu) {
yun_suan_more_cheng_or_chu();
if(jian_mark != 0) {
jian_chu = true;
if(chu_mark == 0) {
sum_2 = num_2;
num_2 = 1;
}
sum_2 = chu_fa(sum_2, num_2);
}
else if(jia_mark != 0) {
jia_chu = true;
if(chu_mark == 0) {
sum_2 = num_2;
num_2 = 1;
}
sum_2 = chu_fa(sum_2, num_2);
}
else if(cheng_mark != 0) {
sum_1 = cheng_fa(sum_1, num_2);
num_2 = sum_1;
}
else if(chu_mark == 0) {
sum_1 = 1.0;
sum_1 = chu_fa(num_2, sum_1);
}
else {
sum_1 = chu_fa(sum_1, num_2);
}
num_1 = 0;
num_2 = 1;
chu_mark++;
cheng_mark = 0;
jian_mark = 0;
jia_mark = 0;
chu = false;
}
}
public void actionPerformed(ActionEvent e) {
set_show( e.getActionCommand() );//不让运算符号重复出现
if(point == 0) {
if( e.getActionCommand() != "+" &&
e.getActionCommand() != "=" &&
e.getActionCommand() != "-"&&
e.getActionCommand() != "×"&&
e.getActionCommand() != "÷"&&
e.getActionCommand() != "."&&
e.getActionCommand() != "C") {
if(wei_shu == 0 && e.getActionCommand() != "<--") {
num_1 = ((reJButton)e.getSource()).i;
num_2 = ((reJButton)e.getSource()).i;
last_2 = ((reJButton)e.getSource()).i;
temp = ((reJButton)e.getSource()).i;
count_jia = 0;
count_jian = 0;
count_chu = 0;
count_cheng = 0;
index = 1;
deng = false;
wei_shu++;
}
else if(wei_shu == 1 && e.getActionCommand() == "<--") {
num_1 = 0;
num_2 = 0;
last_2 = 0;
temp = 0;
wei_shu--;
}
else if(wei_shu >= 1 && e.getActionCommand() != "<--") {
temp = temp * Math.pow(10, index) + ((reJButton)e.getSource()).i;
num_1 = temp;
num_2 = temp;
last_2 = temp;
count_jia = 0;
count_jian = 0;
count_chu = 0;
count_cheng = 0;
wei_shu++;
}
else if(wei_shu > 1 && e.getActionCommand() == "<--") {
index = -1;
temp = (int)(temp * Math.pow(10, index));
num_1 = temp;
num_2 = temp;
last_2 = temp;
wei_shu--;
index = 1;
}
}
}
else {
if( e.getActionCommand() != "+" &&
e.getActionCommand() != "=" &&
e.getActionCommand() != "-"&&
e.getActionCommand() != "×"&&
e.getActionCommand() != "÷"&&
e.getActionCommand() != "."&&
e.getActionCommand() != "C") {
if(index == -1 && e.getActionCommand() == "<--") {
point = 0;
}
else if(index == -1 && e.getActionCommand() != "<--") {
temp2 = temp + ((reJButton)e.getSource()).i*Math.pow(10, index);
num_1 = temp2;
num_2 = temp2;
last_2 = temp2;
count_jia = 0;
count_jian = 0;
count_chu = 0;
count_cheng = 0;
index--;
deng = false;
}
else if(index < -1 && e.getActionCommand() != "<--") {
temp2 = temp2 + ((reJButton)e.getSource()).i*Math.pow(10, index);
num_1 = temp2;
num_2 = temp2;
last_2 = temp2;
index--;
}
else if(index < -1 && e.getActionCommand() == "<--") {
String temp_ss = String.valueOf(temp2);
temp_ss = temp_ss.substring(0, temp_ss.length()-1);
temp2 = Double.parseDouble(temp_ss);
num_1 = temp2;
num_2 = temp2;
last_2 = temp2;
index++;
}
}
}
/**************************************************************************************/
if(((reJButton)e.getSource()).getText() == ".") {
point = 1;
index = -1;
}
if(e.getActionCommand() == "+") {
jia = true;
jian = false;
cheng = false;
chu = false;
count_jia++;
count_jian = 0;
count_chu = 0;
count_cheng = 0;
wei_shu = 0;
point = 0;
last_yun_suan = "+";
index = 0;
}
else if(e.getActionCommand() == "-") {
jia = false;
jian = true;
cheng = false;
chu = false;
count_jian++;
count_jia = 0;
count_cheng = 0;
count_chu = 0;
wei_shu = 0;
point = 0;
last_yun_suan = "-";
index = 0;
}
else if(e.getActionCommand() == "×") {
jia = false;
jian = false;
cheng = true;
chu = false;
count_cheng++;
count_jia = 0;
count_jian = 0;
count_chu = 0;
wei_shu = 0;
point = 0;
last_yun_suan = "×";
index = 0;
}
else if(e.getActionCommand() == "÷") {
jia = false;
jian = false;
cheng = false;
chu = true;
count_chu++;
count_jia = 0;
count_jian = 0;
count_cheng = 0;
wei_shu = 0;
point = 0;
last_yun_suan = "÷";
index = 0;
}
/*****************************************************************************/
suanfa_jia();
suanfa_jian();
suanfa_cheng();
suanfa_chu();
/***************************************************************************/
if(e.getActionCommand() == "=") {
if(last_yun_suan == "+")
sum_1 = jia_fa(sum_1, last_2);
else if(last_yun_suan == "-")
sum_1 = jian_fa(sum_1, last_2);
else if(last_yun_suan == "×") {
if(jian_cheng)
sum_1 = sum_1 - cheng_fa(sum_2, last_2);
else if(jia_cheng)
sum_1 = sum_1 + cheng_fa(sum_2, last_2);
else
sum_1 = cheng_fa(sum_1, last_2);
}
else if(last_yun_suan == "÷") {
if(jian_chu) {
sum_1 = sum_1 - chu_fa(sum_2, last_2);}
else if(jia_chu) {
sum_1 = sum_1 + chu_fa(sum_2, last_2);}
else
sum_1 = chu_fa(sum_1, last_2);
}
ss = "=" + String.valueOf(sum_1);
if(ss.length() > 14) {
ss = ss.substring(0, 13);
}
txt2.setText(ss);//13位显示
deng = true;
more_suan = sum_1;
ss2 = "";
ss = "";
reset();
}
if( ((reJButton)e.getSource()).getText() == "C") {
txt2.setText("");
reset();
}
}
}
public class Test2{
public static void main(String args[]) {
new Calc();
}
}