Java Jpanel,can';设置背景色

Java Jpanel,can';设置背景色,java,swing,jpanel,Java,Swing,Jpanel,我有一个扩展JFrame的主类,然后将一个jpanel添加到JFrame。然后我尝试设置jpanel的背景色,但没有效果。我不确定问题出在哪里,根据我在谷歌上的发现,在JPanel中简单地设置setBackground(Color)应该可以解决这个问题,但它似乎不起作用。其他修复方法还有setOpaque(true)和setVisible(true),或者使用getContentPane().setBackground(Color)形成JFrame,但这些方法似乎都不起作用。如果您有任何建议,我

我有一个扩展JFrame的主类,然后将一个jpanel添加到JFrame。然后我尝试设置jpanel的背景色,但没有效果。我不确定问题出在哪里,根据我在谷歌上的发现,在JPanel中简单地设置
setBackground(Color)
应该可以解决这个问题,但它似乎不起作用。其他修复方法还有
setOpaque(true)
setVisible(true)
,或者使用
getContentPane().setBackground(Color)
形成JFrame,但这些方法似乎都不起作用。如果您有任何建议,我们将不胜感激。如果您需要更多信息或有其他建议,请随时告知我。:) 主要类别为:

public class main extends JFrame{

    private Content content;

    public main(){

        content = new Content(400, 600);

        this.setTitle("Shooter2.0");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.getContentPane().add(content);
        this.getContentPane().setBackground(Color.BLACK);
        this.pack();
        this.setVisible(true);
        try{
            Thread.sleep(10000);
        }catch(Exception e){}
    }


    public static void main(String[] args){
        main game = new main();
    }

}
public class Content extends JPanel{

    private viewItem ship;

    public Content(int w, int h){
        this.setPreferredSize(new Dimension(w, h));
        this.setLayout(new BorderLayout());     
        this.createBattlefield();
        this.setOpaque(true);
        this.setBackground(Color.BLACK);
        this.repaint();
        this.setVisible(true);
    }

    public void createBattlefield(){
        ship = new viewItem("bubble-field.png", 180, 550, 40, 42);      
    }

    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        this.setBackground(Color.BLACK);
        ship.draw(g);       
    }

}
内容类别为:

public class main extends JFrame{

    private Content content;

    public main(){

        content = new Content(400, 600);

        this.setTitle("Shooter2.0");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.getContentPane().add(content);
        this.getContentPane().setBackground(Color.BLACK);
        this.pack();
        this.setVisible(true);
        try{
            Thread.sleep(10000);
        }catch(Exception e){}
    }


    public static void main(String[] args){
        main game = new main();
    }

}
public class Content extends JPanel{

    private viewItem ship;

    public Content(int w, int h){
        this.setPreferredSize(new Dimension(w, h));
        this.setLayout(new BorderLayout());     
        this.createBattlefield();
        this.setOpaque(true);
        this.setBackground(Color.BLACK);
        this.repaint();
        this.setVisible(true);
    }

    public void createBattlefield(){
        ship = new viewItem("bubble-field.png", 180, 550, 40, 42);      
    }

    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        this.setBackground(Color.BLACK);
        ship.draw(g);       
    }

}

您正在覆盖
paint
,而不调用

super.paint(g);
这样可以防止绘制背景和子组件

要在Swing override
paintComponent
中进行自定义绘制,并利用Swing的优化绘制模型,请使用
@override
进行注释并调用
super.paintComponent(g)


更换代码块

public void paint(Graphics g){
    g.setColor(Color.BLACK);
    this.setBackground(Color.BLACK);
    ship.draw(g);       
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);        
    ship.draw(g);       
}
您正在构造函数中设置JPanel的背景色,因此在paintComponent(){}方法中不需要它


试试上面的代码,它肯定会起作用。

paintComponent
使用双缓冲,因此性能更高。