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


在上面的代码中,innerPanel不显示,也没有任何错误发生。任何想法如何显示位于externalPanel内部的innerPanel。
getContentPane()。add(innerPanel)
但这没用。

最佳答案

尝试改变

outerPanel.setLayout(null);




outerPanel.setLayout(new FlowLayout());


或完全删除setLayout调用。

10-05 23:52