JAVA 一个简单的图形界面计算器

时间:2023-01-26 16:59:00
<pre name="code" class="java">package 计算器;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Point;
import java.awt.Toolkit;

public class 计算器 extends JFrame {

/**
*
*/
private static final long serialVersionUID = 7390993416513457117L;
private JPanel contentPane;
private JTextField textField;
private JButton btnC;
// 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private boolean firstDigit = true;
// 计算的中间结果。
private double resultNum = 0.0;
// 当前运算的运算符
private String operator = "=";
// 操作是否合法
private boolean operateValidFlag = true;
//储存临时结果
private double memoryNum = 0.0;
//储存变量中是否有值
private boolean isNum = false;



/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
计算器 frame = new 计算器();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 处理在储存键被按下的事件
*
* @param key
*/
private void handleMemoryNumber(String key) {

if(key.equals("M+")){
memoryNum += getNumberFromText(); //获取当前文本框中的数字,并储存
isNum = true; //改变是否有保存数字的状态为真
textField.setText("0"); //清除文本框显示内容
firstDigit = true; //设置首位数字为真
resultNum = 0.0; //设置中间结果变量为初始值
}else if(key.equals("M-")){
if(isNum){ //首先判断储存变量内部是否有值
memoryNum -= getNumberFromText(); //有值时用原有的值减去当前获取的值
if(memoryNum==0.0){
isNum = false; //减完的差为0时,还原是否有储存值状态为假
}else{
isNum = true; //如果差不为0时,保持储存值状态为真
}
textField.setText("0"); //设置文本框为0
firstDigit = true; //设置首数字为真
resultNum = 0.0; //清除中间结果变量值为初始值
}else{
memoryNum = getNumberFromText(); //储存变量中没有保存数值时,直接获取数值进行保存
if(memoryNum==0.0){ //如果其中数值为0,设置是否有储存数变量为假,否则为真
isNum = false;
}else{
isNum = true;
}
textField.setText("0");
firstDigit = true;
resultNum = 0.0;
}
}else if(key.equals("MR")){ //如果是否储存数据变量为真时,显示储存的数值到文本框,并显示正确的格式
if(isNum){
long t1;
double t2;
t1 = (long) memoryNum;
t2 = memoryNum - t1;
if (t2 == 0) {
textField.setText(String.valueOf(t1));
firstDigit = true;
} else {
textField.setText(String.valueOf(memoryNum));
firstDigit = true;
}
}else{
textField.setText("0"); //如果是否储存数据变量为假时,清除文本框内容为0
firstDigit = true;
}
}else if(key.equals("MC")){
memoryNum = 0.0; //清除储存变量中的数值,并设置为初始值
isNum = false; //设置是否储存数值变量为初始值
textField.setText("0"); //清除文本框显示内容
firstDigit = true;
resultNum = 0.0;
}
}
/**
* 处理数字键被按下的事件
*
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
if (key.equals(".")){
textField.setText("0"+key);
}else if(key.equals("0") ){ //在首位数字状态时,如果按下的是零键,除非是输入第二位操作数时显示为“0”,否则忽略按键操作
//如果是输入第二个操作数时,按下首位的零给予显示
if(operator.equals("-") || operator.equals("+") || operator.equals("*") || operator.equals("/")|| operator.equals("=")){
textField.setText(key);
}else{
return; //如果首位数字按下的是零键时进行忽略
}
}else{
// 输入的第一个数字
textField.setText(key);
}
} else if ((key.equals(".")) && (textField.getText().indexOf(".") < 0)) {

// 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
textField.setText(textField.getText() + ".");

} else if(key.equals("0")){ //非首位数字时按下零键,如果首位已经是“0”时,忽略本次按键操作,否则正常给后位增加“0”
if(textField.getText().equals("0")){
return;
}else{
textField.setText(textField.getText() + key);
}
} else if (!key.equals(".")&&!key.equals("0")) { //按下的键非小数点和0键时,如果首位已经是“0”,用输入键的数字代替首位数字的“0”,否则在原内容后增加“0”
if(textField.getText().equals("0")){
textField.setText(key);
}else{
// 如果输入的不是小数点,则将数字附在结果文本框的后面
textField.setText(textField.getText() + key);
}
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}

/**
* 处理单独的操作符键
*
* @param key
*/
private void handleOperatorOne(String key) {

if(textField.getText().equals("除数不能为零,请重新计算") || textField.getText().equals("零没有倒数")){
return;
}else{
double Num = getNumberFromText();

switch(key){
case "+/-":
//正负号运算
resultNum = Num*(-1);
operateValidFlag();
firstDigit = true;
break;
case "sqrt":
// 平方根运算
resultNum = Math.sqrt(Num);
operateValidFlag();
firstDigit = true;
break;
case "%":
// 百分号运算,除以100
resultNum = Num / 100;
operateValidFlag();
firstDigit = true;
break;
case "1/x":
// 倒数运算
if (Num == 0.0) {
// 操作不合法
textField.setText("零没有倒数");
firstDigit = true;
break;
} else {
resultNum = 1 / Num;
operateValidFlag();
firstDigit = true;
break;
}

default:
break;

}
}
}
/**
* 处理运算符键被按下的事件
*
* @param key
*/
private void handleOperator(String key) {
if(textField.getText().equals("除数不能为零,请重新计算") || textField.getText().equals("零没有倒数")){
resultNum = 0.0;
textField.setText("0");
operateValidFlag = false;
}else if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0

if (getNumberFromText() == 0.0) {
// 操作不合法
operateValidFlag = false;
textField.setText("除数不能为零,请重新计算");
resultNum = 0.0;
} else {
resultNum /= getNumberFromText();

}
}else if (operator.equals("+")) {
// 加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")) {
// 减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")) {
// 乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("=")) {

// 赋值运算
resultNum = getNumberFromText();

}
if (operateValidFlag) {
operateValidFlag();
}
// 运算符等于用户按的按钮
operator = key;
firstDigit = true;
operateValidFlag = true;
}

/*
* 操作有效时输出格式正确的值
*/
private void operateValidFlag(){
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
textField.setText(String.valueOf(t1));
} else {
textField.setText(String.valueOf(resultNum));
}
}
/**
* 从结果文本框中获取数字
*
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(textField.getText()).doubleValue();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, e);
}
return result;
}
/**
* 处理按键C的处理方法
*/
private void handleC() {

/**
* 处理C键被按下的事件
*/

// 初始化计算器的各种值
textField.setText("0");
resultNum = 0.0;
firstDigit = true;
operator = "=";
}
/**
* 处理Backspace键的方法
*/

private void handleBackspace() {
// TODO 自动生成的方法存根
String text = textField.getText();
int i = text.length();
if (i > 0) {
// 退格,将文本最后一个字符去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
// 如果文本没有了内容,则初始化计算器的各种值
textField.setText("0");
firstDigit = true;
operator = "=";
} else {
// 显示新的文本
textField.setText(text);
}
}

}
/**
* 处理CE键的方法
*/
private void handleCE(){
// 用户按了"CE"键
textField.setText("0");
firstDigit = true;
}
/**
* Create the frame.
*/
public 计算器() {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/calc.png")));
setResizable(false);
setLocation(new Point(500, 300));
setTitle("\u8BA1\u7B97\u5668 by \u547C\u7F8E\u8FB0 \u9AD8\u65B0\u4E00\u5C0F\u56DB3\u73ED");
setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 506, 380);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 153, 102));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(new Color(255, 153, 153));
panel.setBounds(10, 10, 481, 332);
contentPane.add(panel);
panel.setLayout(null);

textField = new JTextField();
textField.setEditable(false);
textField.setForeground(new Color(255, 51, 255));
textField.setBackground(Color.WHITE);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("宋体", Font.PLAIN, 24));
textField.setText("0");
textField.setBounds(16, 16, 446, 36);
panel.add(textField);
textField.setColumns(10);

JButton btnSqrt = new JButton("sqrt");
btnSqrt.setFont(new Font("Dialog", Font.PLAIN, 17));
btnSqrt.setForeground(new Color(255, 102, 255));
btnSqrt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperatorOne(btnSqrt.getText());
}
});
btnSqrt.setBounds(401, 120, 61, 37);
panel.add(btnSqrt);

JButton button_1 = new JButton("%");
button_1.setFont(new Font("Dialog", Font.PLAIN, 17));
button_1.setForeground(new Color(255, 102, 255));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperatorOne(button_1.getText());
}
});
button_1.setBounds(401, 173, 61, 37);
panel.add(button_1);

JButton button_2 = new JButton("1/x");
button_2.setFont(new Font("Dialog", Font.PLAIN, 17));
button_2.setForeground(new Color(255, 102, 255));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperatorOne(button_2.getText());
}
});
button_2.setBounds(401, 226, 61, 37);
panel.add(button_2);

JButton button_3 = new JButton("/");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperator(button_3.getText());
}
});
button_3.setFont(new Font("Dialog", Font.PLAIN, 17));
button_3.setForeground(new Color(255, 102, 0));
button_3.setBounds(324, 120, 61, 37);
panel.add(button_3);

JButton button_4 = new JButton("*");
button_4.setFont(new Font("Dialog", Font.PLAIN, 17));
button_4.setForeground(new Color(255, 102, 0));
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperator(button_4.getText());
}
});
button_4.setBounds(324, 173, 61, 37);
panel.add(button_4);

JButton button_5 = new JButton("-");
button_5.setFont(new Font("Dialog", Font.PLAIN, 17));
button_5.setForeground(new Color(255, 102, 0));
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperator(button_5.getText());
}
});
button_5.setBounds(324, 226, 61, 37);
panel.add(button_5);

JButton button_6 = new JButton("+");
button_6.setFont(new Font("Dialog", Font.PLAIN, 17));
button_6.setForeground(new Color(255, 102, 0));
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperator(button_6.getText());
}
});
button_6.setBounds(324, 279, 61, 37);
panel.add(button_6);

JButton button_7 = new JButton("9");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_7.getText());
}
});
button_7.setFont(new Font("Dialog", Font.PLAIN, 17));
button_7.setForeground(new Color(255, 51, 255));
button_7.setBounds(247, 120, 61, 37);
panel.add(button_7);

JButton button_8 = new JButton("6");
button_8.setFont(new Font("Dialog", Font.PLAIN, 17));
button_8.setForeground(new Color(255, 51, 255));
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_8.getText());
}
});
button_8.setBounds(247, 173, 61, 37);
panel.add(button_8);

JButton button_9 = new JButton("3");
button_9.setFont(new Font("Dialog", Font.PLAIN, 17));
button_9.setForeground(new Color(255, 51, 255));
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_9.getText());
}
});
button_9.setBounds(247, 226, 61, 37);
panel.add(button_9);

JButton button_10 = new JButton(".");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_10.getText());
}
});
button_10.setFont(new Font("Dialog", Font.PLAIN, 17));
button_10.setForeground(new Color(255, 51, 255));
button_10.setBounds(247, 279, 61, 37);
panel.add(button_10);

JButton button_11 = new JButton("8");
button_11.setFont(new Font("Dialog", Font.PLAIN, 17));
button_11.setForeground(new Color(255, 51, 255));
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_11.getText());
}
});
button_11.setBounds(170, 120, 61, 37);
panel.add(button_11);

JButton button_12 = new JButton("5");
button_12.setFont(new Font("Dialog", Font.PLAIN, 17));
button_12.setForeground(new Color(255, 51, 255));
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_12.getText());
}
});
button_12.setBounds(170, 173, 61, 37);
panel.add(button_12);

JButton button_13 = new JButton("2");
button_13.setFont(new Font("Dialog", Font.PLAIN, 17));
button_13.setForeground(new Color(255, 51, 255));
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_13.getText());
}
});
button_13.setBounds(170, 226, 61, 37);
panel.add(button_13);

JButton button_14 = new JButton("+/-");
button_14.setFont(new Font("Dialog", Font.PLAIN, 17));
button_14.setForeground(new Color(255, 102, 0));
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperatorOne(button_14.getText());
}
});
button_14.setBounds(170, 279, 61, 37);
panel.add(button_14);

JButton button_15 = new JButton("7");
button_15.setForeground(new Color(255, 51, 255));
button_15.setFont(new Font("Dialog", Font.PLAIN, 17));
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_15.getText());
}
});
button_15.setBounds(93, 120, 61, 37);
panel.add(button_15);

JButton button_16 = new JButton("4");
button_16.setForeground(new Color(255, 51, 255));
button_16.setFont(new Font("Dialog", Font.PLAIN, 17));
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_16.getText());
}
});
button_16.setBounds(93, 173, 61, 37);
panel.add(button_16);

JButton button_17 = new JButton("1");
button_17.setForeground(new Color(255, 51, 255));
button_17.setFont(new Font("Dialog", Font.PLAIN, 17));
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_17.getText());
}
});
button_17.setBounds(93, 226, 61, 37);
panel.add(button_17);

JButton button_18 = new JButton("0");
button_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber(button_18.getText());
}
});
button_18.setForeground(new Color(255, 51, 255));
button_18.setFont(new Font("Dialog", Font.PLAIN, 17));
button_18.setBounds(93, 279, 61, 37);
panel.add(button_18);

JButton button = new JButton("=");
button.setForeground(new Color(255, 102, 0));
button.setFont(new Font("宋体", Font.BOLD, 26));
button.setBounds(401, 279, 61, 37);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOperator(button.getText());
}
});
panel.add(button);
handleOperator(button.getText());

JButton btnMc = new JButton("MC");
btnMc.setForeground(new Color(255, 102, 255));
btnMc.setFont(new Font("宋体", Font.PLAIN, 15));
btnMc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleMemoryNumber(btnMc.getText());
}
});
btnMc.setBounds(16, 120, 61, 37);
panel.add(btnMc);

JButton btnMr = new JButton("MR");
btnMr.setForeground(new Color(255, 102, 255));
btnMr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleMemoryNumber(btnMr.getText());
}
});
btnMr.setFont(new Font("宋体", Font.PLAIN, 15));
btnMr.setBounds(16, 173, 61, 37);
panel.add(btnMr);

JButton btnMs = new JButton("M-");
btnMs.setForeground(new Color(255, 102, 255));
btnMs.setFont(new Font("宋体", Font.PLAIN, 15));
btnMs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleMemoryNumber(btnMs.getText());
}
});
btnMs.setBounds(16, 226, 61, 37);
panel.add(btnMs);

JButton btnM = new JButton("M+");
btnM.setForeground(new Color(255, 102, 255));
btnM.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleMemoryNumber(btnM.getText());
}
});
btnM.setFont(new Font("宋体", Font.PLAIN, 15));
btnM.setBounds(16, 279, 61, 37);
panel.add(btnM);

btnC = new JButton("C");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleC();

}
});
btnC.setForeground(new Color(255, 102, 0));
btnC.setFont(new Font("宋体", Font.PLAIN, 15));
btnC.setBounds(350, 68, 112, 36);
panel.add(btnC);

JButton btnNewButton = new JButton("CE");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleCE();
}
});
btnNewButton.setForeground(new Color(255, 102, 0));
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
btnNewButton.setBounds(226, 68, 112, 36);
panel.add(btnNewButton);

JButton btnBackspace = new JButton("Backspace");
btnBackspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleBackspace();
}

});
btnBackspace.setForeground(new Color(255, 102, 0));
btnBackspace.setFont(new Font("宋体", Font.PLAIN, 15));
btnBackspace.setBounds(102, 68, 112, 36);
panel.add(btnBackspace);

}
}