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();
}
}
Java基本图形用户界面及基本事件实例
最新推荐文章于 2024-07-21 19:30:47 发布