Home>Article>Java> java window background color setting

java window background color setting

王林
王林 Original
2020-01-13 10:36:43 6495browse

java window background color setting

The window background color refers to the color displayed after directly calling the setBackground(Color color) method of JFrame or Frame.

If you call this method directly, the background color is indeed set, but what you see is not the direct JFrame or Frame, but JFrame.getContentPane(), and the contentPane on the JFrame defaults to Color.WHITE . So, no matter how you set the background color for JFrame or Frame, all you see is the contentPane.

Recommended related video tutorials:java video tutorials

Solution:

Method 1: After completing the initialization, call getContentPane () method gets a contentPane container, and then sets it to invisible, that is, setVisible(false).

The code is as follows:

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); //设置窗口可见 } }

Method 2: Directly add this.getContentPane().setBackground(Color.blue);

The code is as follows:

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); } }

Recommended related articles and tutorials:java introductory tutorial

The above is the detailed content of java window background color setting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn