您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。
如果直接调用这个方法后,的确设置了背景颜色,但看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane(),而JFrame上的contentPane默认是Color.WHITE的。所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane。
解决方法:
方法一:在完成初始化,调用getContentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。
代码如下:
import javax.swing.*;
import java.awt.*
public class TestMenuBar1 {
	public static void main(String arg[]) {
		createNewMenu ck=new createNewMenu("第一个窗口");
	}
}
class createNewMenu extends JFrame{
	public createNewMenu(String title) {
		getContentPane().setVisible(false);
		setBackground(Color.blue);  //设置窗口背景颜色
		setTitle(title);
		setBounds(200,200,500,500); //设置窗口位置和大小
		setVisible(true);  //设置窗口可见
	}
}方法二:直接加 this.getContentPane().setBackground(Color.blue);
代码如下:
import java.awt.*;
import javax.swing.*;
public class TestMenuBar1 {
	public static void main(String arg[]) {
		createNewMenu ck=new createNewMenu("第一个窗口");
	}
}
class createNewMenu extends JFrame{
	public createNewMenu(String title) {
		setTitle(title);
		setBounds(200,200,500,500);
		setVisible(true);
		this.getContentPane().setBackground(Color.blue);
	}
}以上就是java窗口背景颜色设置的详细内容,更多请关注亿速云其它相关文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。