package Demo1;
/**
*
* @author Administrator
*
*/
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class Test3 extends JFrame implements ActionListener
{
public static void main(String[] args)
{
Test3 t=new Test3();
}
MyPanel1 mp=new MyPanel1();
JButton jb1=null;
JButton jb2=null;
public Test3()
{
mp=new MyPanel1();
Cat cat=new Cat();
mp.setBackground(Color.black);
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb1.setActionCommand("a");
jb1.addActionListener(cat);
jb2.addActionListener(this);
jb2.addActionListener(cat);
jb2.setActionCommand("b");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
}public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("a"))
{
System.out.println("你点了黑丝按钮");
mp.setBackground(Color.black);
}else if(e.getActionCommand().equals("b"))
{
System.out.println("你点了红色");
mp.setBackground(Color.red);
}
}
}
class MyPanel1 extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent d) {
if(d.getActionCommand().equals("a"))
{
System.out.println("猫也知道你点了黑色");
}else if(d.getActionCommand().equals("b"));
{
System.out.println("猫也知道你点了红色");
}
}
}我点了黑色 怎么会出现这样结果:
“猫也知道你点了黑色
猫也知道你点了红色
你点了黑丝按钮”
/**
*
* @author Administrator
*
*/
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class Test3 extends JFrame implements ActionListener
{
public static void main(String[] args)
{
Test3 t=new Test3();
}
MyPanel1 mp=new MyPanel1();
JButton jb1=null;
JButton jb2=null;
public Test3()
{
mp=new MyPanel1();
Cat cat=new Cat();
mp.setBackground(Color.black);
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb1.setActionCommand("a");
jb1.addActionListener(cat);
jb2.addActionListener(this);
jb2.addActionListener(cat);
jb2.setActionCommand("b");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
}public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("a"))
{
System.out.println("你点了黑丝按钮");
mp.setBackground(Color.black);
}else if(e.getActionCommand().equals("b"))
{
System.out.println("你点了红色");
mp.setBackground(Color.red);
}
}
}
class MyPanel1 extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent d) {
if(d.getActionCommand().equals("a"))
{
System.out.println("猫也知道你点了黑色");
}else if(d.getActionCommand().equals("b"));
{
System.out.println("猫也知道你点了红色");
}
}
}我点了黑色 怎么会出现这样结果:
“猫也知道你点了黑色
猫也知道你点了红色
你点了黑丝按钮”
解决方案 »
- java中的字节流和字符流到底是怎么回事
- 郁闷的,HttpURLConnection通过POST访问WCF REST服务,100分送!
- String底层问题。。。
- 在myeclipse中如何将JTable中的内容导出成excel,急!急
- 迷惘迷惘...
- HTML页面提交的值,JAVASCRIPT如何取得,我需要实现JAVASC RIPT操作数据库,表单中提交的内容,插入数据库,现在我不能取到提交后的值,没有后
- 100分求助北京java培训!!!
- 运行jar 出现问题......
- 在一个类的内部可以使用他自己的一个实例吗?
- 请问什么地方有JAVA 的联机文档下啊????
- java offscreen 的问题。。
- java怎样随机选取一个集合里面的数.
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener
{
public static void main(String[] args)
{
Test t=new Test();
}
MyPanel1 mp=new MyPanel1();
JButton jb1=null;
JButton jb2=null;
public Test()
{
mp=new MyPanel1();
Cat cat=new Cat();
mp.setBackground(Color.black);
jb1=new JButton("黑色");
jb2=new JButton("红色");
this.add(jb1,BorderLayout.NORTH);
this.add(mp);
this.add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb1.setActionCommand("a");
jb1.addActionListener(cat);
jb2.addActionListener(this);
jb2.addActionListener(cat);
jb2.setActionCommand("b");
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("a"))
{
System.out.println("你点了黑丝按钮");
mp.setBackground(Color.black);
}
else if(e.getActionCommand().equals("b"))
{
System.out.println("你点了红色");
mp.setBackground(Color.red);
}
}
}
class MyPanel1 extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
}
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent d) {
if(d.getActionCommand().equals("a"))
{
System.out.println("猫也知道你点了黑色");
}
else if(d.getActionCommand().equals("b"))
//}else if(d.getActionCommand().equals("b"));
{
System.out.println("猫也知道你点了红色");
}
}
}
{
System.out.println("猫也知道你点了红色");的 else if(d.getActionCommand().equals("b")); 多了个分号。另外,ActionListener的调用顺序是从上到下,即先按钮后JFrame