我用 java 写一个 计算器 怎么实现 按钮事件呢??、/ 该用API 中的什么方法 怎么用啊???
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame {
boolean flag=false;
private TextField textfield;
public Calculator(){
super("Calculator");
setResizable(false);
setBounds(200,200,300,200);
Container container=getContentPane();
JPanel content=new JPanel();
textfield=new TextField(30);
container.setBackground(Color.cyan);
content.setLayout(new GridLayout(4,4,10,10));
String s="1,2,3,4,5,6,7,8,9,0,=,c,-,+,*,/";
String button[]=s.split(",");
/*for(int i=0;i<button.length;i++){
System.out.println((button[i]));
}
*/
// for(int i=0;i<button.length;i++){
// content.add(new JButton(button[i]));
// }
JButton b1=new JButton(button[0]);
JButton b2=new JButton(button[1]);
JButton b3=new JButton(button[2]);
JButton b4=new JButton(button[3]);
JButton b5=new JButton(button[4]);
JButton b6=new JButton(button[5]);
JButton b7=new JButton(button[6]);
JButton b8=new JButton(button[7]);
JButton b9=new JButton(button[8]);
JButton b10=new JButton(button[9]);
JButton b11=new JButton(button[10]);
JButton b12=new JButton(button[11]);
JButton b13=new JButton(button[12]);
JButton b14=new JButton(button[13]);
JButton b15=new JButton(button[14]);
JButton b16=new JButton(button[15]);
content.add(b1);
content.add(b2);
content.add(b3);
content.add(b4);
content.add(b5);
content.add(b6);
content.add(b7);
content.add(b8);
content.add(b9);
content.add(b10);
content.add(b11);
content.add(b12);
content.add(b13);
content.add(b14);
content.add(b15);
content.add(b16);
b1.addActionListener(a1);
b2.addActionListener(a2);
b3.addActionListener(a3);
b4.addActionListener(a4);
b5.addActionListener(a5);
b6.addActionListener(a6);
b7.addActionListener(a7);
b8.addActionListener(a8);
b9.addActionListener(a9);
b10.addActionListener(a10);
b11.addActionListener(a11);
b12.addActionListener(a12);
b13.addActionListener(a13);
b14.addActionListener(a14);
b15.addActionListener(a15);
b16.addActionListener(a16);
container.add(content,BorderLayout.SOUTH);
container.add(new JScrollPane(textfield),BorderLayout.NORTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Btn1Listener a1 = new Btn1Listener();
Btn2Listener a2 = new Btn2Listener();
Btn3Listener a3 = new Btn3Listener();
Btn4Listener a4 = new Btn4Listener();
Btn5Listener a5 = new Btn5Listener();
Btn6Listener a6 = new Btn6Listener();
Btn7Listener a7 = new Btn7Listener();
Btn8Listener a8 = new Btn8Listener();
Btn9Listener a9 = new Btn9Listener();
Btn10Listener a10 = new Btn10Listener();
Btn11Listener a11 = new Btn11Listener();
Btn12Listener a12 = new Btn12Listener();
Btn13Listener a13 = new Btn13Listener();
Btn14Listener a14 = new Btn14Listener();
Btn15Listener a15 = new Btn15Listener();