不能正确的改变颜色.
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
public class FrameTest extends JFrame implements ActionListener
{
JButton bt1=new JButton("红色");
JButton bt=new JButton("1234567");
JButton bt2=new JButton("绿色");
FrameTest()
{
setTitle("单击事件");
setSize(300,300);
setBackground(Color.white);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
JPanel pan=new JPanel();
pan.setLayout(new FlowLayout());
bt1.addActionListener(this);
bt2.addActionListener(this);
pan.add(bt);
pan.add(bt1);
pan.add(bt2);
setContentPane(pan);
setVisible(true);
}
public static void main(String args[])
{
JFrame frame=new FrameTest();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt1)
setBackground(Color.red);
if(e.getSource()==bt2)
setBackground(Color.cyan);
}
}
为什么我点下bt1或bt2,颜色一闪而过,并不能改变啊,是怎么回事?