java同时监听监听_java中如何同时监听多个事件,下面是我写的code,我想实现分别点击a,b,c按钮分别出现不同的行为,谢谢...

importjava.awt.*;importjava.awt.event.*;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjavax.swing.*;publicclassListenerimplementsAction...

import java.awt.*;

import java.awt.event.*;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import javax.swing.*;

public class Listener implements ActionListener {

private JFrame frame;

private JTextField tf;

private JButton[] allButtons;

private JButton a;

private JButton b;

private JButton c;

public static void main(String[] args) {

Listener two = new Listener();

two.init();

}

public void init() {

frame = new JFrame("Calculator");

Container contentPane = frame.getContentPane();

contentPane.add(new Label("计算器"), BorderLayout.NORTH);

frame.setLayout(new BorderLayout());

//Container contentPane = frame.getContentPane();

JPanel centerPanel =new JPanel();

JPanel northPanel =new JPanel();

JPanel southPanel =new JPanel();

southPanel.setLayout(new GridLayout(4,4));

tf = new JTextField(30);

centerPanel.add(tf);

for (int i = 0; i < allButtons.length; i++) {

allButtons[i].addActionListener(this);}

a = new JButton("words");

b = new JButton("show");

c = new JButton("change");

allButtons = new JButton[16];

String str = "123+456-789*0.=/";

for (int i = 0; i < 16; i++) {

allButtons[i] = new JButton(str.substring(i, i + 1));

}

southPanel.setLayout(new GridLayout(4, 4));

northPanel.setLayout(new GridLayout(1, 3));

frame.add(southPanel, BorderLayout.SOUTH);

for (int i = 0; i < allButtons.length; i++) {

southPanel.add(allButtons[i]);

}

northPanel.add(a);

northPanel.add(b);

northPanel.add(c);

frame.add(southPanel,BorderLayout.CENTER);

frame.add(northPanel,BorderLayout.SOUTH);

contentPane.add(tf, BorderLayout.NORTH);

frame.setSize(400, 400);

frame.setVisible(true);

// 注册监听程序

a.addActionListener(this);

b.addActionListener(this);

c.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

String s = "This is my first java program and thank you for your support";

tf.setText(s);

c.setBackground(Color.CYAN);

}

}

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值