您的位置 首页 技术

java窗口背景颜色设置

窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。 如果直接调用这个方法后,的确设置了背景颜色,但看…

窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。

如果直接调用这个方法后,的确设置了背景颜色,但看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane(),而JFrame上的contentPane默认是Color.WHITE的。所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane。

推荐相关视频教程:java视频教程

解决方法:

方法一:在完成初始化,调用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入门教程

以上就是java窗口背景颜色设置的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/26792.html

为您推荐

返回顶部