简体   繁体   English

Java Jpanel,无法设置背景色

[英]Java Jpanel, can't set background color

I have a main class that extends a JFrame, which then adds a jpanel to the jframe. 我有一个扩展JFrame的主类,然后将Jpanel添加到jframe。 Then I try to set the background color of the jpanel, but to no avail. 然后我尝试设置jpanel的背景颜色,但无济于事。 I'm not really sure where the problem is, according to what i've found on google, simply setting setBackground(Color) in the JPanel should fix this but it doesn't seem to work. 根据我在google上找到的信息,我不太确定问题出在哪里,只需在JPanel中设置setBackground(Color)解决此问题,但似乎不起作用。 Also other fixes for this were setOpaque(true) , and setVisible(true) , or form the JFrame using getContentPane().setBackground(Color) But none of these seem to work. 其他对此的修复是setOpaque(true)setVisible(true) ,或使用getContentPane().setBackground(Color)形成JFrame,但这些方法似乎都不起作用。 Any suggestions would be very appreciated, and if you need more info, or have other advice, please feel free to enlighten me. 任何建议将不胜感激,如果您需要更多信息,或有其他建议,请随时赐教。 :) The main class is: :)主要的类是:

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

}

and the Content class is: 而Content类是:

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

}

You're overriding paint without calling 您在不打电话的情况下覆盖paint

super.paint(g);

This prevents the background and child components from being painted. 这样可以防止背景和子组件被绘制。

For custom painting in Swing override paintComponent instead and take advantage of Swing's optimized paint model, annotate with @Override and invoke super.paintComponent(g) . 对于在Swing重写风俗画paintComponent 代替 ,并充分利用Swing的优化油漆模式,与注释@Override和调用super.paintComponent(g)

Performing Custom Painting 进行定制绘画

Replace the code block 更换代码块

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

By 通过

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);        
    ship.draw(g);       
}

you are setting the background color of JPanel in constructor so there is no need it in paintComponent(){} method... 您正在构造函数中设置JPanel的背景色,因此paintComponent(){}方法中不需要它...

try above code it will surely works.... 试试上面的代码,它肯定会工作。

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

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