package com.test3;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Demo9_3 extends JFrame implements ActionListener{
JPanel mp=null;
JButton jb1,jb2;
Cat mycat;
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo9_3 demo9_3=new Demo9_3();
}
public Demo9_3()
{
mp=new JPanel();
mycat=new Cat();
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
mp.setBackground(Color.blue);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
//注册监听,jb1是事件源,可以添加很多监听事件的监听器
jb1.addActionListener(this);
//jb1事件源,mycat是事件监听对象,事件是ActionEvent e
jb1.addActionListener(mycat);
//指定action命令
jb1.setActionCommand("heise");
jb2.addActionListener(this);
jb2.addActionListener(mycat);
jb2.setActionCommand("hongse");
this.setSize(200,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("heise"))
mp.setBackground(Color.black);
if(e.getActionCommand().equals("hongse"))
mp.setBackground(Color.red);
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getActionCommand().equals("heise"))
System.out.println("heise");
if(arg0.getActionCommand().equals("hongse"))
System.out.println("hongse");
}
}
//class MyPanel extends JPanel
//{
// public void paint(Graphics g)
// {
// super.paint(g);
// }
//}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Demo9_3 extends JFrame implements ActionListener{
JPanel mp=null;
JButton jb1,jb2;
Cat mycat;
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo9_3 demo9_3=new Demo9_3();
}
public Demo9_3()
{
mp=new JPanel();
mycat=new Cat();
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
mp.setBackground(Color.blue);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
//注册监听,jb1是事件源,可以添加很多监听事件的监听器
jb1.addActionListener(this);
//jb1事件源,mycat是事件监听对象,事件是ActionEvent e
jb1.addActionListener(mycat);
//指定action命令
jb1.setActionCommand("heise");
jb2.addActionListener(this);
jb2.addActionListener(mycat);
jb2.setActionCommand("hongse");
this.setSize(200,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("heise"))
mp.setBackground(Color.black);
if(e.getActionCommand().equals("hongse"))
mp.setBackground(Color.red);
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getActionCommand().equals("heise"))
System.out.println("heise");
if(arg0.getActionCommand().equals("hongse"))
System.out.println("hongse");
}
}
//class MyPanel extends JPanel
//{
// public void paint(Graphics g)
// {
// super.paint(g);
// }
//}