【发布时间】:2012-09-21 12:52:49
【问题描述】:
我想创建一个包含一些 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);
}
}
【问题讨论】:
-
1) 不要扩展组件,只保留对它们的引用。 2) 为了尽快获得更好的帮助,请发帖SSCCE。
-
对不起,为什么不扩展呢?在我的情况下,最好有一个现成的组件来保存我的标签,以免代码复杂
-
我要读这篇文章。谢谢。
标签: java swing inheritance jpanel composition