I have a GUI im building to illustrate a tree. The JPanel that I extended isnt showing the JLabel that I created when I constructed the JPanel. Here is my JPanel code:
class NodePanel extends JPanel {
private JLabel content;
NodePanel() {
this.setLayout(new FlowLayout());
this.setBorder(new LineBorder(Color.black));
}
NodePanel(String str) {
this.setLayout(new FlowLayout());
this.setBorder(new LineBorder(Color.black));
content = new JLabel(str);
this.add(content);
}
}
and here is the code where im testing it:
NodePanel n1 = new NodePanel(Integer.toString(root.getWeight()));
HuffmanGUI.tp.add(n1);
n1.setBounds((tpWidth / 2) - (panel开发者_C百科Dim / 2), vertSpace, panelDim, panelDim);
This code is in a method called by an action listener, I know it works because the panel shows up where its supposed to, but the Jlabel isnt displayed inside it. Why could this be?
Its hard to tell because we don't know the context of how the code is used. It looks like you are mixing null layouts with layout managers.
I might guess that you are adding the panel to a visible GUI and you haven't used revalidate() on the panel so the label doesn't have a size.
If you need more help then post your SSCCE that demonstrates the problem.
精彩评论