java多个按钮监听,如何改成监听多个按钮啊用e.getsource()

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import javax.swing.JFrame;

import javax.swing.JTextField;

import java.awt.Font;

import java.awt.event.*;

import javax.swing.JButton; public class calFrame extends JFrame{

protected static String sign="+";

protected static float no1;

protected static int jud=1,jud2=0;

protected static StringBuffer text = new StringBuffer();//可伸缩字符串

protected static JTextField text_out=new JTextField(10);

public calFrame() {

super("简易计算器");

int i;

JButton buttons[]=new JButton[18];

final String fuhao2="0.=+123-456x789÷←C";

text_out.setHorizontalAlignment(JTextField.RIGHT);

text_out.setBounds(0,20,385,50);

text_out.setFont(new Font("黑体",100,50));

text_out.setText("0");

this.add(text_out);

this.setLayout(null);//自由布局

for(i=1;i<19;i++){

buttons[i-1]=new JButton(""+fuhao2.charAt(i-1));//创建按钮若干

this.add(buttons[i-1]);

buttons[i-1].setBounds(100*((i%4==0?4:i%4)-1),50*(9-((i-1)/4))-160,80,40);

buttons[i-1].setFont(new Font("楷体",5,40));//设置按钮字体

}

buttons[0].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('0');

}});

buttons[1].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add(fuhao2.charAt(1));

}});

buttons[2].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

deal("null",Float.parseFloat(text_out.getText()));

}});

buttons[3].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

deal("+",Float.parseFloat(text_out.getText()));

}});

buttons[4].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('1');

}});

buttons[5].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('2');

}});

buttons[6].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('3');

}});

buttons[7].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

deal("-",Float.parseFloat(text_out.getText()));

}});

buttons[8].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('4');

}});

buttons[9].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('5');

}});

buttons[10].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('6');

}});

buttons[11].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

deal("x",Float.parseFloat(text_out.getText()));

}});

buttons[12].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('7');

}});

buttons[13].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('8');

}});

buttons[14].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

add('9');

}});

buttons[15].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

deal("÷",Float.parseFloat(text_out.getText()));

}});

buttons[16].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

text.deleteCharAt(text.length()-1);//退格

text_out.setText(text.toString());

}});

buttons[17].addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

text.delete(0,100);

no1=0;

text_out.setText("0");

sign="+";

}});

}

public float calculate(float a,float b,String sign){

if(sign.equals("+")){return a+b;}

if(sign.equals("-")){return a-b;}

if(sign.equals("x")){return a*b;}

if(sign.equals("÷")){return a/b;}

return a;

}

public void deal(String sign2,float no2){//计算方法

if(jud==1){no1=calculate(no1,no2,sign);

text_out.setText(String.valueOf(no1));

text.delete(0,100);

sign=sign2;}

else text_out.setText("输入错误!C清屏");

jud=0;

}

public void add(char c){

text.append(""+c);

text_out.setText(text.toString());

jud=1;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值