I am facing a problem that i think its pretty easy to solve but i cannot figure it out.
So i have a main jpanel and i cannot insert another jpanel2 in a different class to it, the components from jpanel2 dont appear on the 1st one.
Here's the code for the 1st jpanel constructor:
public PainelPrincipal(Jogo janela) {`
super();
painel = new JPanel(){
protected void paintComponent(java.awt.Graphics g) {
super.paintComponents(g);
try{
g.drawImage(ImageIO.read(PainelPrincipal.class.getResource("Imagens/fundo/Fundo0.jpg")), 0, 0, this);
}catch(IOException e){
`e.printStackTrace();
}
};
};
painel.setPreferredSize(new Dimension(1024, 768));
janela.setContentPane(painel);
painel.setLayout(null);
painelBonus = new PainelBonus();
painelBonus.setBounds(199, 537, 352, 156);
painel.add(painelBonus);
painelVida = new PainelVida();
painelVida.setBounds(856, 426, 73, 267);
//painelVida.setBounds();
painel.add(painelVida);
lblPontuacao = new JLabel("Pontua\u00E7\u00E3o: 0");
lblPontuacao.setForeground(new Color(255, 69, 0));
lblPontuacao.setBounds(0, 0, 1024, 22);
lblPontuacao.setBackground(new Color(128, 0, 0));
lblPontuacao.setOpaque(true);
lblPontuacao.setHorizontalAlignment(SwingConstants.CENTER);
painel.add(lblPontuacao);
JLabel labelEsq = new JLabel("");
labelEsq.setBackground(new Color(128, 0, 0));
labelEsq.setOpaque(true);
labelEsq.setBounds(0, 21, 11, 747);
painel.add(labelEsq);
GridPanel gridPanel_1 = new GridPanel();
gridPanel_1.setBounds(10, 33, 767, 418);
gridPanel_1.setShowGridLines(true);
gridPanel_1.setRowSize(40);
gridPanel_1.setColumnSize(40);
gridPanel_1.setColumns(18);
painel.add(gridPanel_1);
JLabel labelDir = new JLabel("");
labelDir.setOpaque(true);
labelDir.setBackground(new Color(128, 0, 0));
labelDir.setBounds(1013, 21, 11, 747);
painel.add(labelDir);
}
This code painelBonus = new PainelBonus(); executes the constructor PainelBonus with all the components:
public PainelBonus() {
super();
painel = new JPanel();
painel.setBackground(new Color(0, 0, 0));
painel.setBorder(null);
painel.setPreferredSize(new Dimension(300, 157));
painel.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);
painel.add(btnImagemMartelo);
JButton btnImagemBomba = new JButton("");
btnImagemBomba.setBounds(154, 11, 136, 136);
btnImagemBomba.setIcon(imagemBomba);
btnImagemBomba.setContentAreaFilled(false);
painel.add(btnImagemBomba);
}
Here's the problem: the components from PainelBonus doesn't seem to appear on PainelPrincipal
Screenshot: http://imgur.com/2wdZAOW
Sorry for bad formatting, kinda new here :D
TY Hovercraft Full Of Eels for the help on editing :D
PainelBonus
, which seems to extend fromJPanel
(or some otherComponent
based class) creates it's ownJPanel
(painel = new JPanel();
) to which you add some components, but you never add that panel (painel
) toPainelBonus
Equally,
PainelPrincipal
creates aJPanel
,painel
onto which you add other components, includingPainelBonus
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