简易计算器,顾名思义,只能进行一次计算,没有记忆功能
自创一个类继承ActionListener,重写actionPerformed(ActionEvent e)方法,实现计算器的功能,
getSource()方法返回值是组件对象名,getActionCommand()方法返回值是组件对象的标签。
比如一个Button b2 = new JButton(“haha”);
e.getSource()返回 b2,e.getActionCommand() 返回haha字符串。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test1 extends JFrame{
JButton a0,a1,a2,a3,a4,a5,a6,a7,a8,a9;
JButton b1,b2,b3,b4;
JButton c1,c2,c3,c4;
JTextField t1,t2;
JPanel p1,p2;
JLabel l1,l2;
String op;
double num1,num2;
boolean ch,cliks;
public static void main(String[] args) {
Test1 p = new Test1();
p.cal();
}
public Test1() {
a1 = new JButton("1");
a2 = new JButton("2");
a3 = new JButton("3");
a4 = new JButton("4");
a5 = new JButton("5");
a6 = new JButton("6");
a7 = new JButton("7");
a8 = new JButton("8");
a9 = new JButton("9");
a0 = new JButton("0");
b1 = new JButton("清空");
b2 = new JButton("返回");
b3 = new JButton(".");
b4 = new JButton("=");
c1 = new JButton("+");
c2 = new JButton("-");
c3 = new JButton("*");
c4 = new JButton("/");
t1 = new JTextField();
t2 = new JTextField();
l1 = new JLabel("结");
l2 = new JLabel("果");
p1 = new JPanel();
p1.setLayout(new GridLayout(2,3,2,2));
p1.add(t1);
p1.add(b1);
p1.add(b2);
p1.add(t2);
p1.add(l1);
p1.add(l2);
this.add(p1,BorderLayout.NORTH);
p2 = new JPanel(new GridLayout(4,4,2,2));
p2.add(a1);
p2.add(a2);
p2.add(a3);
p2.add(c1);
p2.add(a4);
p2.add(a5);
p2.add(a6);
p2.add(c2);
p2.add(a7);
p2.add(a8);
p2.add(a9);
p2.add(c3);
p2.add(b3);
p2.add(a0);
p2.add(b4);
p2.add(c4);
this.add(p2,BorderLayout.CENTER);
this.setTitle("计算器");
this.setBounds(700,400,400,300);
this.setVisible(true);
}
public void cal() {
bu_listener ls = new bu_listener();
a1.addActionListener(ls);
a2.addActionListener(ls);
a3.addActionListener(ls);
a4.addActionListener(ls);
a5.addActionListener(ls);
a6.addActionListener(ls);
a7.addActionListener(ls);
a8.addActionListener(ls);
a9.addActionListener(ls);
a0.addActionListener(ls);
b1.addActionListener(ls);
b2.addActionListener(ls);
b3.addActionListener(ls);
b4.addActionListener(ls);
c1.addActionListener(ls);
c2.addActionListener(ls);
c3.addActionListener(ls);
c4.addActionListener(ls);
}
class bu_listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Object temp = e.getSource();
if(temp==a1||temp==a2||temp==a3||temp==a4||temp==a5||temp==a6||
temp==a7||temp==a8||temp==a9||temp==a0) {
if (ch == true) {
t1.setText("");
t2.setText("");
}
t1.setText(t1.getText() + "" + e.getActionCommand());
ch = false;
} else if(temp==c1||temp==c2||temp==c3||temp==c4){
num1 = Double.parseDouble(t1.getText());
t1.setText("");
op = e.getActionCommand();
} else if(temp==b3) {
cliks = true;
for(int i = 0; i < t1.getText().length(); i++) {
if('.'==t1.getText().charAt(i)) {
cliks = false;
break;
}
}
if(cliks==true) {
t1.setText(t1.getText()+".");
}
} else if(temp==b4) {
num2 = Double.parseDouble(t1.getText());
t1.setText(num1+" "+op+" "+num2+" = ");
if("+".equals(op)) t2.setText(num1+num2+"");
else if("-".equals(op)) t2.setText(num1*num2+"");
else if("*".equals(op)) t2.setText(num1*num2+"");
else {
if(num2==0) {
t2.setText("除数不能为0");
return;
}
t2.setText(num1/num2+"");
}
} else if(temp==b1) {
t1.setText("");
t2.setText("");
} else if(temp==b2) {
String cnt = t1.getText();
t1.setText("");
StringBuffer sb = new StringBuffer();
for(int i = 0; i < cnt.length()-1; i++) {
sb = sb.append(cnt.charAt(i));
}
t1.setText(sb.toString());
}
}
}
}