JFrame这样的框架,一旦创建,在其中就已经包含一个内容面板,一般我们在往JFrame中添加组件时,都加在了内容面板中,这个面板可以通过JFrame的成员方法getContentPane()取出来,所以如果设置JFrame的背景颜色,仍然会被内容面板盖住,不如设置内容面板的背景颜色当时如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class test{
JFrame frame;
test() {
frame=new JFrame();
frame.setSize(200,400);
frame.setVisible(true); // frame.setBackground(Color.green);如果这样设置背景色,不起作用。
frame.getContentPane().setBackground(Color.green);
frame.setResizable(false);
}
public static void main(String args[]) {
new test();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class test{
JFrame frame;
test() {
frame=new JFrame();
frame.setSize(200,400);
frame.setVisible(true); // frame.setBackground(Color.green);如果这样设置背景色,不起作用。
frame.getContentPane().setBackground(Color.green);
frame.setResizable(false);
}
public static void main(String args[]) {
new test();
}
}