简体   繁体   English

Frame.getContentPane()。setBackground(Color.black)不

[英]Frame.getContentPane().setBackground(Color.black) doesn't

I've followed the advice of countless other tutorials for changing the background color of a JFrame, but I haven't any luck. 我遵循了无数其他教程的建议来更改JFrame的背景颜色,但是我没有任何运气。 Here's my code for initializing and creating a frame 这是我用于初始化和创建框架的代码

package frameTests;

import javax.swing.*;

import java.awt.*;


@SuppressWarnings("serial")
public class Foo extends JPanel {

public static void frameInit(Foo foobar){
    // window initialization
    JFrame frame = new JFrame("Spaceship Defenders!");
    frame.add(foobar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    frame.getContentPane().setBackground(Color.black);
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);// center frame in screen.
}




public static void main(String[] args) throws InterruptedException{

    Foo bar = new Foo();
    frameInit(bar);

    }
}

这是结果框,带有默认的灰色背景

Your panel foobar is hiding the ContentPane . 您的面板foobar隐藏了ContentPane Either make the panel transparent or set the color of the panel itself to BLACK 使面板透明或将面板本身的颜色设置为BLACK

foobar.setOpaque(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM