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没有显示,也没有发生任何错误。知道如何显示位于outerPanel内部的innerPanel。我尝试使用 getContentPane().add(innerPanel) 但没有成功。
最佳答案
尝试改变
outerPanel.setLayout(null);
至
outerPanel.setLayout(new FlowLayout());
或完全删除 setLayout 调用。
关于java - JPanel 未显示在另一个 JPanel 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234509/