我有一个主类扩展了一个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);
}
}