//一个小型计算器,自己的一些思考还有借鉴了别人的一些代码然后写出了自己java的最后一个作业计算器
package gui.hbut.edu.cn.www;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display; //这个编辑框用来显示数据
JPanel jPanel;
JPanel numbersPanel;
final static int ADD = 1; //对加,减,乘,除用常量进行存取,易于操作
final static int SUB = 2;
final static int MULT = 3;
final static int DIV = 4;
private double number1 = 0.0,number2 = 0.0,result = 0.0;
private int operation = 0;
private boolean isCalculating = false; //检测状态
final private String [] buttonText = { "←", "CE", "C", "+", "7", "8", "9", "-", "4",
"5", "6", "*", "1", "2", "3", "/", "+/-", "0", ".", "=" }; //用来排版
public Calculator(){
jPanel = new JPanel();
display = new JTextField("0");
display.setPreferredSize(new Dimension(240, 35)); //编辑框格式
display.setFont(new Font("", Font.PLAIN, 20));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
jPanel.add(display, "North");
numbersPanel = new JPanel();
numbersPanel.setLayout(new GridLayout(5,4,12,25));
for(int i = 0; i < buttonText.length; i++){
JButton button = new JButton(buttonText[i]);
numbersPanel.add(button);
button.addActionListener(this);
}
jPanel.add(numbersPanel, "South");
getContentPane().add(jPanel);
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setBounds(100,100,250,320);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand(); //获取鼠标点击操作符
if(Character.isDigit(cmd.charAt(0)) || cmd.equals(".")){
if(isCalculating){
display.setText("0");
isCalculating = false;
}
if(display.getText().equals("0")){ //屏幕为0
if(cmd.equals("0"))
return ;
else if(cmd.equals("."))
display.setText("0.");
else
display.setText(cmd);
}else{ //屏幕不为0
if(!display.getText().contains("."))
display.setText(display.getText() + cmd);
else if(!cmd.equals(".")){
display.setText(display.getText() + cmd);
}
}
}else{
if(cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")){
number1 = Double.parseDouble(display.getText());
display.setText("");
if(cmd.equals("+"))
operation = ADD;
else if(cmd.equals("-"))
operation = SUB;
else if(cmd.equals("*"))
operation = MULT;
else
operation = DIV;
}
else if(cmd.equals("C")){ //所有数进行清空
number1 = 0;
number2 = 0;
result = 0;
display.setText("0");
}
else if(cmd.equals("CE")){ //清除正在输入的数
if(number2 == 0)
number1 = 0;
else
number2 = 0;
}
else if(cmd.equals("←")){ //退格符
if(display.getText().length() == 1)
display.setText("0");
else
display.setText(display.getText().substring(0, display.getText().length()-1));
}
else if (cmd.equals("+/-")) { //改变屏幕输入数的正负
if (display.getText().equals("0")) {
return;
}
if (display.getText().contains("-")) {
display.setText(display.getText().substring(1));
} else {
display.setText("-" + display.getText());
}
}
else if(cmd.equals("="))
{
number2 = Double.parseDouble(display.getText());
switch (operation) {//按操作运算符号常量分支
case 1:
result = number1 + number2;
break;
case 2:
result = number1 - number2;
break;
case 3:
result = number1 * number2;
break;
case 4:
result = number1 / number2;
break;
default:
result = number2;
}
Number resultObject = result;
if ((resultObject.doubleValue() - resultObject.intValue()) < 1e-10) {
String resultString = resultObject.intValue() + "";
display.setText(resultString);
}
else
display.setText(result + "");
result = 0.0;
number1 = 0.0;
number2 = 0.0;
}
}
}
public static void main(String []args){
Calculator calculator = new Calculator();
}
}
用java写计算器
最新推荐文章于 2023-09-19 12:31:51 发布