程序一:直接调用setBackground(Color.GREEN) 背景颜色没有变成绿色!!!
程序二:使用了getContentPane()方法 背景颜色成功变成绿色
// 程序一
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {
setLayout(null);
setBackground(Color.GREEN);
setBounds(40,50,480,320);
setVisible(true);
}
public static void main(String[] args) {
Test t = new Test();
}
}
// 程序二
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {
Container c = this.getContentPane();
c.setLayout(null);
c.setBackground(Color.GREEN);
setBounds(40,50,480,320);
setVisible(true);
}
public static void main(String[] args) {
Test t = new Test();
}
}
|