【发布时间】:2015-08-21 17:21:46
【问题描述】:
一些背景:我正在制作一个程序,它使用一个充满各种类型节点的 JTree。 TreeSelectionListener 确定它是什么类型的节点,清除组件的内部 JPanel,然后将适当的 JPanel 添加到它刚刚清除的 JPanel,以编辑特定于该类型节点的属性。 JPanel 使用 GridBagLayout,但文本字段显示不正确(注意:标题为 JTextField,描述为 JTextArea:
JPanel 的代码如下:
class nodePanel extends JPanel{
public nodePanel(DefaultMutableTreeNode info){
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets=new Insets(10,10,10,10);
c.gridx=0;
c.gridy=0;
c.gridheight=1;
c.gridwidth=1;
c.weightx=0;
c.weighty=0;
c.fill=GridBagConstraints.NONE;
c.anchor=GridBagConstraints.EAST;
this.add(new JLabel("Title: "),c);
c.gridy=1;
c.weighty=2;
c.gridheight=2;
this.add(new JLabel("Description: "),c);
c.gridx=1;
c.gridy=0;
c.gridheight=1;
c.gridwidth=2;
c.weightx=100;
c.weighty=0;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
JTextField title = new JTextField();
title.setMinimumSize(new Dimension(300,25));
this.add(title,c);
c.gridy=1;
c.gridheight=2;
c.weighty=200;
JTextArea description = new JTextArea();
JScrollPane descriptionScroll= new JScrollPane(description);
descriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
descriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
this.add(descriptionScroll, c);
}
}
谢谢
【问题讨论】:
-
另外,对于节点本身来说,
.toString()方法就是这种节点的标题,所以应该在标题区显示“Example Child Node”。
标签: java swing jtextfield jtextarea gridbaglayout