在swing中如何设置组件的背景色。在如下代码中,红色闪一下就没了,谁知道应该怎么做吗?谢谢!(分不够了,对不起)import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
         {
             this.setSize(400,400);
    this.setBackground(Color.red);
} public static void main(String[] args)
{
new Test().setVisible(true);
}
}

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;
    public class Test extends JFrame
    {
            public Test()
             {
                 this.setSize(400,400);
               // this.setBackground(Color.red);
            try {
                jbInit();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }        public static void main(String[] args)
            {
                    new Test().setVisible(true);
            }    private void jbInit() throws Exception {
            this.getContentPane().setBackground(Color.red);
        }
    }
      

  2.   

    this.getContentPane().setBackground(Color.red);  关键是这一句
      

  3.   

    我还想问JLable的背景色问题;
    import java.awt.*;
    import javax.swing.*;
    public class Test extends JFrame
    {

    public Test(){
    JLabel jl=new JLabel();
    this.setSize(400,400);
    this.getContentPane().add(jl);
    jl.setBackground(Color.red);

             jl.setOpaque(true);
                          //为什么加上这一句就可以设置为红色,这句什么意思。
        

    } public static void main(String[] args)
    {
    new Test().setVisible(true);
    }
    }
      

  4.   

    setOpaque(true);如果为 true,则该组件绘制其边界内的所有像素。否则该组件可能不绘制其某些或所有像素,从而允许其下面的像素透视出来。
      

  5.   

    Container container=getContentPane();
    container.setBackground(Color.RED);
      

  6.   

    opaque
    n.
    不透明物
    adj.
    不透明的, 不传热的, 迟钝的