简体   繁体   English

另一个Jpanel中的Jpanel

[英]Jpanel inside another Jpanel

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. 所以我有一个主jpanel,我不能在另一个类中插入另一个jpanel2,jpanel2的组件没有出现在第一个。
Here's the code for the 1st jpanel constructor: 这是第一个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);
}

This code painelBonus = new PainelBonus(); 这段代码painelBonus = new PainelBonus(); executes the constructor PainelBonus with all the components: 用所有组件执行构造函数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);
}

Here's the problem: the components from PainelBonus doesn't seem to appear on PainelPrincipal 这是问题所在:PainelBonus的组件似乎没有出现在PainelPrincipal上

Screenshot: http://imgur.com/2wdZAOW 截图: http//imgur.com/2wdZAOW

Sorry for bad formatting, kinda new here :D 对不起,格式不好,这是新的:D
TY Hovercraft Full Of Eels for the help on editing :D TY气垫船充满鳗鱼,可帮助您编辑:D

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 PainelBonus似乎是从JPanel (或其他一些基于Component的类)扩展而来的,它创建了自己的JPanelpainel = new JPanel(); ),并向其中添加了一些组件,但从未将面板( painel )添加到PainelBonus

Equally, PainelPrincipal creates a JPanel , painel onto which you add other components, including PainelBonus but this panel ( painel ) is never added to anything. 同样, PainelPrincipal创建一个JPanelpainel ,您可以在其中添加其他组件,包括PainelBonus但此面板( painel )永远不会添加到任何东西。

Don't use null layouts. 不要使用null布局。 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. 完美的像素布局是现代UI设计中的一种错觉,您无法控制字体,DPI,渲染管线或其他因素,这些因素会改变组件在屏幕上的渲染方式。

Swing was designed to work with layout managers to overcome these issues. Swing旨在与布局经理一起克服这些问题。 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... 如果您坚持不理会这些功能并反对API设计,请为许多麻烦做好准备,并且永不结束艰苦的工作...

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM