简易计算器,顾名思义,只能进行一次计算,没有记忆功能
自创一个类继承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