简体   繁体   English

JPanel没有出现在另一个JPanel中

[英]JPanel does not show up inside another JPanel

class ABC extends JFrame {
    public JPanel createGUI()
    {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(null);

        JLabel top = new JLabel();
        top.setBounds(40,40,400,30);
        top.setText("Hello World");
        outerPanel.add(top);

        int l = getLength();
        JPanel innerPanel = new JPanel();
        if(l==0)
        {
            innerPanel.setLayout(null);
            JLabel empty = new JLabel("No Data Found");
            empty.setBounds(80,150,300,30);
            innerPanel.add(empty);
        }
        else
        {
            innerPanel.setLayout(new GridLayout(l,4,5,5)); 
            for(int i=0;i<l;i++)
            {
                innerPanel.add(new JLabel("Text1");
                innerPanel.add(new JLabel("Text2");

                JButton b1 = new JButton("Button1");
                innerPanel.add(b1);
                JButton b2 = new JButton("Button2");
                innerPanel.add(b2);
            }          
         }
         outerPanel.add(innerPanel);
         return outerPanel;
    }
}

In the above code the innerPanel does not show up and neither any error occurs.Any idea how to show up the innerPanel which is inside an outerPanel.I tried using getContentPane().add(innerPanel) but it didn't work. 在上面的代码中,innerPanel没有显示出来,也没有任何错误发生。任何想法如何显示位于externalPanel内部的innerPanel。我尝试使用getContentPane()。add(innerPanel),但是没有用。

Try changing 尝试改变

outerPanel.setLayout(null);

to

outerPanel.setLayout(new FlowLayout());

or remove the setLayout call entirely. 或完全删除setLayout调用。

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

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