Jpanel在另一个Jpanel里面

时间:2014-04-30 21:19:03

标签: java swing 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

1 个答案:

答案 0 :(得分:0)

PainelBonus,似乎从JPanel(或其他基于Component的类)延伸,创建了自己添加的JPanelpainel = new JPanel();)某些组件,但您从未将该面板(painel)添加到PainelBonus

同样,PainelPrincipal会创建JPanelpainel,您可以在其中添加其他组件,包括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);
}