为什么label不会变色呢
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Work4 extends JFrame implements ActionListener{
private JButton btn1,btn2,btn3;
private JLabel lbl;
private Container cp;
private GridLayout layout;
public Work4(){
super("bianselong");
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn1=new JButton("红色");
btn2=new JButton("黄色");
btn3=new JButton("兰色");
lbl=new JLabel("the color will be changed");
cp=getContentPane();
layout=new GridLayout(3,1,6,6);
cp.setLayout(layout);
cp.add(btn1);
cp.add(btn2);
cp.add(btn3);
cp.add(lbl);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1)
{
lbl.setBackground(Color.red);
lbl.setText("红色");}
else if(e.getSource()==btn2)
{lbl.setText("黄色");
lbl.setBackground(Color.yellow);}
else {lbl.setText("兰色");
lbl.setBackground(Color.blue);}
}
public static void main(String[] args){
Work4 work =new Work4();
work.setVisible(true);
}
} 而且输出也不事按照Grid 布局输出得 如果把这个程序改成 Frame 得而步是 JFrame 得就可以变色
[此贴子已经被作者于2006-3-31 14:27:33编辑过]