实训作业——窗口

package bbb;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class ccc extends JFrame implements ActionListener {
JFrame f;
JPanel p;
JButton b1,b2,b3;
public static void main(String[] args) {
ccc tb = new ccc();
}

public ccc(){
p = new JPanel();
b1 = new JButton("Red");  
b2 = new JButton("Green");
b3 = new JButton("Blue");
this.add(p);
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(this);
b1.setActionCommand("Red");
b2.addActionListener(this);
b2.setActionCommand("Green");
b3.addActionListener(this);
b3.setActionCommand("Blue");
this.setTitle("点击按钮");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Red")){

p.setBackground(Color.RED);  
}else if(e.getActionCommand().equals("Green")){

p.setBackground(Color.GREEN);
}else if(e.getActionCommand().equals("Blue")){

p.setBackground(Color.BLUE);
}
}

}

转载于:https://www.cnblogs.com/A9999999/p/11028521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值