ButtonDemo

package java_learning;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class button_test{
    public static void main(String[]args){
        ButtonDemo myButtonGUI=new ButtonDemo();//声明并创建按钮对象
        myButtonGUI.setVisible(true);
    }
}
class ButtonDemo extends JFrame implements ActionListener{
    public static final int Width=250;
    public static final int Height=200;
    ButtonDemo(){
        setSize(Width,Height); 
        setTitle("按钮事件样例");
        Container conPane=getContentPane();
        conPane.setBackground(java.awt.Color.yellow);
        conPane.setLayout(new FlowLayout());//采用FlowLayout布局
        JButton redBut=new JButton("Red");
        redBut.addActionListener(this);//给Red按钮注册监视器
        conPane.add(redBut);//在窗口添加Red按钮
        JButton greenBut=new JButton("Green");
        greenBut.addActionListener(this);//给Green按钮注册监视器
        conPane.add(greenBut);//在窗口添加Green按钮
    }
    public void actionPerformed(ActionEvent e){//实现接口处理事件的方法
        Container conPane=getContentPane();
        if(e.getActionCommand().equals("Red"))//是Red按钮事件
            conPane.setBackground(java.awt.Color.RED);
        else if(e.getActionCommand().equals("Green"))//是Green按钮事件
            conPane.setBackground(java.awt.Color.WHITE);
        else{}
        conPane.setBackground(java.awt.Color.BLUE);
    } 
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值