Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java AWT setBackground_Java_Swing_User Interface_Colors_Awt - Fatal编程技术网

如何使用Java AWT setBackground

如何使用Java AWT setBackground,java,swing,user-interface,colors,awt,Java,Swing,User Interface,Colors,Awt,下面是一些创建基本java窗口的代码: JPanel pane = new JPanel(); gui(String title){ super(title); setBounds(100,100,500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container con = this.getContentPane(); * con.setBackground(new Color(0,0,0)

下面是一些创建基本java窗口的代码:

JPanel pane = new JPanel();
gui(String title){
    super(title);
    setBounds(100,100,500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
*   con.setBackground(new Color(0,0,0));
    con.add(pane);
    setVisible(true);
}
用星号(*)标记的线表示窗口的背景色为黑色(0,0,0)。然而,这条线似乎毫无作用。(我试过在这里使用
窗格.setBackground
,但没有什么不同。)


如何更改背景颜色?

您已在
JFrame
上添加了
JPanel
,它完全屏蔽了设置颜色的基础容器

您可以这样做:

public Gui(String title) {
   super(title);
   JPanel pane = new JPanel();
   setBounds(100, 100, 500, 500);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container con = this.getContentPane();
   pane.setBackground(new Color(0, 0, 0));
   con.add(pane);
   setVisible(true);
}

不能为JFrame的内容窗格设置背景色。 我的意思是:

 JFrame f = new JFrame() ;
 f.setBackground(Color.RED) ;
但是,您应该创建一个JPanel作为您的背景,并将其颜色设置为:

backgroundPanel.setBackground(颜色:红色)


这也可以解决手头的问题。

我尝试添加它,但没有得到任何更改。我应该把它放在哪里?它应该放在星号*所在的地方。这没什么区别吗?知道为什么吗?这对我很有用。我已经发布了我正在使用的完整构造函数。啊,代码的另一部分导致它变成白色。很抱歉,我试着加上了,但没有区别。我应该把它放在哪里?为什么不能为
JFrame
的内容窗格设置背景色
JFrame#setBackground
JFrame#getContentPane#setBackground
对我来说很好,你是对的。这是JFrame.setBackground(Color.RED)。当您看到此评论时,我将删除我的答案。@MadProgrammer将这样做:为了更快地获得帮助,请发布一条消息。
Jframe f =new Jframe();
f.setBackground(Color.red);