0
我有一個圖形用戶界面的建築來說明一棵樹。我擴展的JPanel不顯示我在構建JPanel時創建的JLabel。這裏是我的JPanel代碼:Swing Jlabel沒有顯示
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);
}
}
和這裏是IM測試它的代碼:
NodePanel n1 = new NodePanel(Integer.toString(root.getWeight()));
HuffmanGUI.tp.add(n1);
n1.setBounds((tpWidth/2) - (panelDim/2), vertSpace, panelDim, panelDim);
此代碼是在通過一個動作偵聽器調用的方法,我知道它的工作原理是因爲面板顯示在它應該達到的地方,但Jlabel沒有顯示在裏面。爲什麼會這樣呢?
你叫它。不調用revalidate()是問題。 – Bill