Java基本图形用户界面及基本事件实例

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class show extends JFrame implements ActionListener{
    private JButton cat,dog;    //定义按钮
	public void simpleframe() {
		this.setSize(400, 200);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  //窗口退出
		this.setVisible(true);    //窗口可视化
		this.setTitle("动物");    //定义窗口标题
		cat=new JButton("猫");
	    dog=new JButton("狗");
		cat.setSize(100, 50);
		cat.setLocation(50, 40);
		dog.setSize(100, 50);
		dog.setLocation(200, 40);
		this.add(cat);
		this.add(dog);
		cat.addActionListener((ActionListener) this);//添加监听事件
		dog.addActionListener((ActionListener) this);
	}
	public void actionPerformed(ActionEvent e) {   //事件定义
		if(e.getSource()==cat) {
			animal cat_a=new cat();
		    cat_a.showanimal(cat_a);   //显示猫的名字
		}
		    else if(e.getSource()==dog) {
		    	animal dog_a=new dog();
		    	dog_a.showanimal(dog_a);   //显示狗的名字
		    }
			
	
	}
    public static void main(String[] args) {
       show  a=new show();
       a.simpleframe();
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值