import java.awt.Color;import javax.swing.JFrame;//javax/swing/JFrame.class
import javax.swing.JLabel;
public class JFrameHelloWorld { public static void main(String[] args) {
JFrame win=new JFrame("Hello");//创建一个窗口
win.setBackground(Color.blue);

JLabel jl=new JLabel("Hi JAVA");
jl.setBackground(Color.red);

win.add(jl);
win.setSize(500,200);//数字单位是像素
win.setVisible(true);
// if(isMan){
// System.out.println("爷们来啦");
// } }}
为什么背景颜色不显示啊?请高手赐教

解决方案 »

  1.   

    加上这几句:
    Container cp=win.getContentPane();
    cp.setBackground(Color.blue);cp.add(jl);
    去掉:
    win.setBackground(Color.blue);
    win.add(jl);
    记住,JFrame自身不能添加任何组件,必需得通过Container对象实现。
      

  2.   

    import java.awt.Color;import javax.swing.JFrame;//javax/swing/JFrame.class
    import javax.swing.JLabel;public class JFrameHelloWorld {    public static void main(String[] args) {
            JFrame win = new JFrame("Hello");//创建一个窗口        win.getContentPane().setBackground(Color.blue);//这个是加的        JLabel jl = new JLabel("Hi JAVA");
            jl.setBackground(Color.red);        jl.setOpaque(true);//这个是加的        win.add(jl);
            win.setSize(500, 200);//数字单位是像素
            win.setVisible(true);
        }
    }
    你的JLabel会挡住背景所以一片红
      

  3.   

    我一般喜欢继承JFrame类……那样不需要getComponent……
      

  4.   

    继承JFrame这样个人感觉还是比较方便的……你试试吧。