java jpanel颜色_Java Jpanel,无法设置背景颜色

我有一个主类扩展了一个JFrame,然后JFrame添加一个jpanel。然后我尝试设置jpanel的背景颜色,但无济于事。根据我在google上发现的情况,我不确定问题出在哪里,只需在JPanel中设置setBackground(Color)即可解决此问题,但似乎无法解决问题。此外,其他修补程序为setOpaque(true)和setVisible(true),或者使用getContentPane().setBackground(Color)形成JFrame但这些都不起作用。任何建议将非常感激,如果您需要更多信息,或有其他建议,请随时为我提供启发。 :) 主类是:Java Jpanel,无法设置背景颜色

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值