import java.awt.*;
import java.awt.event.*;public class TestPanel {
public static void main(String args[]) {
W w = new W();
}
}class Mypanel extends Panel implements ActionListener {
Button b1,b2;
Color backColor;
Mypanel() {
Button b1 = new Button("确定");
Button b2 = new Button("取消");
add(b1);
add(b2);
setBackground(Color.cyan);
backColor = getBackground();
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
setBackground(Color.yellow);
}
else if(e.getSource()==b2) {
setBackground(backColor);
}
}
}class W extends Frame implements ActionListener {
Mypanel p1,p2;
Button exit;
W() {
setLayout(new FlowLayout());
p1 = new Mypanel();
p2 = new Mypanel();
exit = new Button("退出");
add(p1);
add(p2);
add(exit);
exit.addActionListener(this);
setBounds(50,50,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
if(e.getSource()==b1) {这行代码 条件判断不会为真~~~找下原因吧
import java.awt.event.*;public class TestPanel {
public static void main(String args[]) {
W w = new W();
}
}class Mypanel extends Panel implements ActionListener {
Button b1,b2;
Color backColor;
Mypanel() {
b1 = new Button("确定");
b2 = new Button("取消");
add(b1);
add(b2);
setBackground(Color.cyan);
backColor = getBackground();
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1)) {
System.out.println("K1");
this.setBackground(Color.yellow);
this.repaint();
}
else if(e.getSource()==b2) {
System.out.println("K2");
setBackground(backColor);
}
}
}class W extends Frame implements ActionListener {
Mypanel p1,p2;
Button exit;
W() {
setLayout(new FlowLayout());
p1 = new Mypanel();
p2 = new Mypanel();
exit = new Button("退出");
add(p1);
add(p2);
add(exit);
exit.addActionListener(this);
setBounds(50,50,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
import java.awt.event.*;public class TestPanel {
public static void main(String args[]) {
W w = new W();
}
}class Mypanel extends Panel implements ActionListener {
Button b1,b2;
Color backColor;
Mypanel() {
//请注意这里:楼主这里定义的是局部变量~~~~楼主粗心了。
b1 = new Button("确定");
b2 = new Button("取消");
add(b1);
add(b2);
setBackground(Color.cyan);
backColor = getBackground();
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1)) {
System.out.println("K1");
this.setBackground(Color.yellow);
this.repaint();
}
else if(e.getSource()==b2) {
System.out.println("K2");
setBackground(backColor);
}
}
}class W extends Frame implements ActionListener {
Mypanel p1,p2;
Button exit;
W() {
setLayout(new FlowLayout());
p1 = new Mypanel();
p2 = new Mypanel();
exit = new Button("退出");
add(p1);
add(p2);
add(exit);
exit.addActionListener(this);
setBounds(50,50,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
Button b1,b2;
Color backColor;
Mypanel() {
Button b1 = new Button("确定");
Button b2 = new Button("取消");
Button b1,b2;
Color backColor;
Mypanel() {
b1 = new Button("确定");
b2 = new Button("取消");
b1是成员变量,e.getSource()获得的是局部变量
e.getSource()==b1永远是false