日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/9/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多个按钮监听,如何改成监听多个按钮啊用e.getsource() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓

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));//創(chuàng)建按鈕若干

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;

}

}

總結

以上是生活随笔為你收集整理的java多个按钮监听,如何改成监听多个按钮啊用e.getsource()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。