简体   繁体   English

为什么我的JFrame的背景不会变黑?

[英]Why won't the background of my JFrame become black?

I'm working on a school project and I'm having trouble changing the background colour.我正在做一个学校项目,但无法更改背景颜色。 For some reason it keeps showing up as the default grey colour instead of black.由于某种原因,它一直显示为默认的灰色而不是黑色。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Class;
import java.lang.reflect.*;


//creates PoolTable claa
public class PoolTable extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
    //creates size of the play screen.

    public JFrame myFrame = new JFrame("Pool!");
    private JPanel panel = new JPanel();
    private int gameWidth;
    private int gameHeight;


        public PoolTable()
        {  
            int width = 750;
            int height = 500;    
            myFrame.setTitle("Pool");
            myFrame.getContentPane().setBackground(Color.BLACK);
            myFrame.setSize(width, height);
            myFrame.setResizable(false);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setContentPane(this);
            myFrame.setVisible(true);


        }   


}

You are replacing default content pane with yours one.您正在用您的替换默认内容窗格。 so change background color after replacing content pane.所以更换内容窗格后更改背景颜色。

        myFrame.setContentPane(this);
        myFrame.getContentPane().setBackground(Color.BLACK);

OR do或者做

        this.setBackground(Color.BLACK);

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

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