[英]Jpanel inside another Jpanel
我面臨一個我認為很容易解決的問題,但我無法解決。 所以我有一個主jpanel,我不能在另一個類中插入另一個jpanel2,jpanel2的組件沒有出現在第一個。
這是第一個jpanel構造函數的代碼:
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);
}
這段代碼painelBonus = new PainelBonus(); 用所有組件執行構造函數PainelBonus:
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);
}
這是問題所在:PainelBonus的組件似乎沒有出現在PainelPrincipal上
截圖: http : //imgur.com/2wdZAOW
對不起,格式不好,這是新的:D
TY氣墊船充滿鰻魚,可幫助您編輯:D
PainelBonus
似乎是從JPanel
(或其他一些基於Component
的類)擴展而來的,它創建了自己的JPanel
( painel = new JPanel();
),並向其中添加了一些組件,但從未將面板( painel
)添加到PainelBonus
同樣, PainelPrincipal
創建一個JPanel
, painel
,您可以在其中添加其他組件,包括PainelBonus
但此面板( painel
)永遠不會添加到任何東西。
不要使用null
布局。 完美的像素布局是現代UI設計中的一種錯覺,您無法控制字體,DPI,渲染管線或其他因素,這些因素會改變組件在屏幕上的渲染方式。
Swing旨在與布局經理一起克服這些問題。 如果您堅持不理會這些功能並反對API設計,請為許多麻煩做好准備,並且永不結束艱苦的工作...
更新了一個例子
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.