我想创建一个包含一些 JLabel 的 JPanel 子类。我开始编写我的代码,但我立即发现了一个大问题。添加到 JPanel 子类的组件不可见(或者它们未添加到 JPanel 我不知道)。这是 JPanel 子类的代码:
public class ClientDetails extends JPanel
{
private JLabel nameAndSurname = new JLabel ("Name & Surname");
private JLabel company = new JLabel ("Company");
private JPanel topPanel = new JPanel ();
public ClientDetails ()
{
this.setBackground(Color.white);
this.setLayout(new BorderLayout());
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
topPanel.add(nameAndSurname);
topPanel.add(company);
this.add(topPanel,BorderLayout.PAGE_START);
}
}