package com.test4;
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
Calculator_Frame cf = new Calculator_Frame();
}
}
//代码效果参考:http://www.zidongmutanji.com/zsjx/401135.html
class Calculator_Frame extends JFrame implements ActionListener {
boolean readyNextNumber = true;
double number1 = 0;
double number2 = 0;
char op = '\0';
JLabel jl;
JPanel jp;
JButton jb_1, jb_2, jb_3, jb_4, jb_5, jb_6, jb_7, jb_8, jb_9, jb_0, jb_jia,
jb_jian, jb_cheng, jb_chu, jb_dian, jb_deng;
String str;
Calculator_Frame() {
jl = new JLabel("0.0", JLabel.RIGHT);
jl.setFont(new Font("Dialog", 1, 17));
jp = new JPanel(new GridLayout(4, 4));
jb_0 = new JButton("0");
jb_9 = new JButton("9");
jb_1 = new JButton("1");
jb_2 = new JButton("2");
jb_3 = new JButton("3");
jb_4 = new JButton("4");
jb_5 = new JButton("5");
jb_6 = new JButton("6");
jb_7 = new JButton("7");
jb_8 = new JButton("8");
jb_jia = new JButton("+");
jb_jian = new JButton("-");
jb_cheng = new JButton("*");
jb_chu = new JButton("/");
jb_deng = new JButton("=");
jb_dian = new JButton(".");
jb_0.addActionListener(this);
jb_1.addActionListener(this);
jb_2.addActionListener(this);
jb_3.addActionListener(this);
jb_4.addActionListener(this);
jb_5.addActionListener(this);
jb_6.addActionListener(this);
jb_7.addActionListener(this);
jb_8.addActionListener(this);
jb_9.addActionListener(this);
jb_jia.addActionListener(this);
jb_jian.addActionListener(this);
jb_cheng.addActionListener(this);
jb_chu.addActionListener(this);
jb_dian.addActionListener(this);
jb_deng.addActionListener(this);
jp.add(jb_1);
jp.add(jb_2);
jp.add(jb_3);
jp.add(jb_jia);
jp.add(jb_4);
jp.add(jb_5);
jp.add(jb_6);
jp.add(jb_jian);
jp.add(jb_7);
jp.add(jb_8);
jp.add(jb_9);
jp.add(jb_cheng);
jp.add(jb_0);
jp.add(jb_dian);
jp.add(jb_deng);
jp.add(jb_chu);
this.add(jl, BorderLayout.NORTH);
this.add(jp, BorderLayout.CENTER);
this.setBounds(100, 10, 200, 300);
this.setDefaultCloseOperation(3);
this.setVisible(true);
public void actionPerformed(ActionEvent e) {
str = e.getActionCommand();// 获取按钮上的字符串
// 判断是否点击的数字
//代码效果参考:http://www.zidongmutanji.com/bxxx/306676.html
if (str.equals("0") || str.equals("1") || str.equals("2")
|| str.equals("3") || str.equals("4") || str.equals("5")
|| str.equals("6") || str.equals("7") || str.equals("8")
|| str.equals("9") || str.equals(".")) {
if (readyNextNumber) {// 判断是否想输入一个新数字
jl.setText(str);
readyNextNumber = false;
} else {
jl.setText(jl.getText() + str);// 把label上的字符串加上刚刚点击的字符串
}
// 判断是否点击的操作符
} else if (str.equals("+")) {
readyNextNumber = true;
if(op == '\0'){
number1 = Double.parseDouble( jl.getText());
}else{
number2 = Double.parseDouble( jl.getText());
jl.setText(count()+"");
number1 = count();
op = '+';
}else if(str.equals("-")){
op = '-';
}else if(str.equals("*")){
op = '*';
}else if(str.equals("/")){
op = '/';
}else if(str.equals("=")){
number2 = Double.parseDouble( jl.getText());
jl.setText(count()+"");
op = '\0';
}
public double count(){
double result = 0;
switch (op) {
case '+':
result= number1 + number2;
break;
case '-':
result=number1 - number2;
case '*':
result=number1 * number2;
case '/':
result=number1 / number2;
default:
return result;