PainelBonus
, which seems to extend from JPanel
(or some other Component
based class) creates it's own JPanel
(painel = new JPanel();
) to which you add some components, but you never add that panel (painel
) to PainelBonus
Equally, PainelPrincipal
creates a JPanel
, painel
onto which you add other components, including PainelBonus
but this panel (painel
) is never added to anything.
Don't use null
layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen.
Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work...
Updated with an example
public PainelPrincipal(Jogo janela) {`
super();
painel = new JPanel(){
protected void paintComponent(java.awt.Graphics g) {
super.paintComponents(g);
try{
// You shouldn't be loading resources in the paint method
// And infact, this could be achieved by using a JLabel instead
// of creating a custom JPanel
g.drawImage(ImageIO.read(PainelPrincipal.class.getResource("Imagens/fundo/Fundo0.jpg")), 0, 0, this);
}catch(IOException e){
e.printStackTrace();
}
};
};
//...
setLayout(new BorderLayout());
add(painel);
}
public PainelBonus() {
super();
setBackground(new Color(0, 0, 0));
setBorder(null);
setPreferredSize(new Dimension(300, 157));
setLayout(null);
imagemMartelo = new ImageIcon(PainelBonus.class.getResource("/Imagens/bonus/bonus_martelo/bonus_martelo_0.png"));
imagemBomba = new ImageIcon(PainelBonus.class.getResource("/Imagens/bonus/bonus_bomba/bonus_bomba_0.png"));
JButton btnImagemMartelo = new JButton("");
btnImagemMartelo.setBounds(10, 11, 136, 136);
btnImagemMartelo.setIcon(imagemMartelo);
btnImagemMartelo.setContentAreaFilled(false);
add(btnImagemMartelo);
JButton btnImagemBomba = new JButton("");
btnImagemBomba.setBounds(154, 11, 136, 136);
btnImagemBomba.setIcon(imagemBomba);
btnImagemBomba.setContentAreaFilled(false);
add(btnImagemBomba);
}