我用 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();
Btn16Listener a16= new Btn16Listener();
public static void main(String args[]){
new Calculator();
}
class Btn1Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{
textfield.setText("1"); }
}
class Btn2Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("2"); }
}
class Btn3Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("3"); }
}
class Btn4Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("4"); }
}
class Btn5Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("5"); }
}
class Btn6Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("6"); }
}
class Btn7Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("7"); }
}
class Btn8Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("8"); }
}
class Btn9Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("9"); }
}
class Btn10Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("0"); }
}
class Btn11Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{
textfield.setText("");
}
}
class Btn12Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn13Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn14Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn15Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn16Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
} }不要小数点 给我个示范 就行了!!! 求求大师们 帮帮忙
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();
Btn16Listener a16= new Btn16Listener();
public static void main(String args[]){
new Calculator();
}
class Btn1Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{
textfield.setText("1"); }
}
class Btn2Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("2"); }
}
class Btn3Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("3"); }
}
class Btn4Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("4"); }
}
class Btn5Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("5"); }
}
class Btn6Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("6"); }
}
class Btn7Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("7"); }
}
class Btn8Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("8"); }
}
class Btn9Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("9"); }
}
class Btn10Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText("0"); }
}
class Btn11Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{
textfield.setText("");
}
}
class Btn12Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn13Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn14Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn15Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
}
class Btn16Listener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ textfield.setText(""); }
} }不要小数点 给我个示范 就行了!!! 求求大师们 帮帮忙
import java.awt.*;
import java.awt.event.*;public class Calculator extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
boolean flag = false;
private TextField textfield;
int op = 0 ;//1是加,2是减,3是乘,4是除
int num1 ; //第一个操作数
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();
Btn16Listener a16 = new Btn16Listener(); public static void main(String args[]) {
new Calculator();
} class Btn1Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("1");
}
} class Btn2Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("2");
}
} class Btn3Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("3");
}
} class Btn4Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("4");
}
} class Btn5Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("5");
}
} class Btn6Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("6");
}
} class Btn7Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("7");
}
} class Btn8Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("8");
}
} class Btn9Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("9");
}
} class Btn10Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("0");
}
} class Btn11Listener implements ActionListener {//等于
public void actionPerformed(ActionEvent e) {
int num2 = Integer.parseInt(textfield.getText()) ;
if(op==1){
int result = num1+num2;
textfield.setText(String.valueOf(result));
}
} } class Btn12Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
} class Btn13Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
} class Btn14Listener implements ActionListener {// 加法
public void actionPerformed(ActionEvent e) {
op = 1;
num1 = Integer.parseInt(textfield.getText()) ;
// textfield.setText("");
}
} class Btn15Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
} class Btn16Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
textfield.setText("");
}
}}