GridBagLayout 将 JTextField 和 JTextArea 显示为短的垂直线
GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
一些背景:我正在制作一个程序,该程序使用充满各种类型节点的 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);
}
}
谢谢
好的,问题不在于 JPanel 本身 - 封闭的 JPanel 有 Gridbag,但没有设置重量或任何尺寸。我猜它会表明问题出在您最意想不到的地方。
// ..
JTextField title = new JTextField(20); // suggest a size in columns!
//title.setMinimumSize(new Dimension(300,25)); // don't call these methods! (1)
this.add(title,c);
c.gridy=1;
c.gridheight=2;
c.weighty=200;
JTextArea description = new JTextArea(3, 15); // suggest a size in rows/cols!
// ..
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
它是因为你没有给 it.If 长度,你在其中添加长度它会起作用 fine.Replace 你的代码用这个
JTextField title = new JTextField(10);
JTextArea description = new JTextArea(2,10);
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.ipadx=20;
c.gridheight=1;
c.gridwidth=2;
c.weightx=100;
c.weighty=0;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
JTextField title = new JTextField(10);
title.setMinimumSize(new Dimension(300,25));
this.add(title,c);
c.gridy=1;
c.gridheight=2;
c.ipadx=20;
c.ipady=5;
c.weighty=200;
JTextArea description = new JTextArea(5,2);
JScrollPane descriptionScroll= new JScrollPane(description);
descriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
descriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
this.add(descriptionScroll, c);
}
}
使用此代码
我改变了你的代码。你没有使用 c.ipadx
我正在添加这个字段,它应该可以工作。
一些背景:我正在制作一个程序,该程序使用充满各种类型节点的 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);
}
}
谢谢
好的,问题不在于 JPanel 本身 - 封闭的 JPanel 有 Gridbag,但没有设置重量或任何尺寸。我猜它会表明问题出在您最意想不到的地方。
// ..
JTextField title = new JTextField(20); // suggest a size in columns!
//title.setMinimumSize(new Dimension(300,25)); // don't call these methods! (1)
this.add(title,c);
c.gridy=1;
c.gridheight=2;
c.weighty=200;
JTextArea description = new JTextArea(3, 15); // suggest a size in rows/cols!
// ..
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
它是因为你没有给 it.If 长度,你在其中添加长度它会起作用 fine.Replace 你的代码用这个
JTextField title = new JTextField(10);
JTextArea description = new JTextArea(2,10);
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.ipadx=20;
c.gridheight=1;
c.gridwidth=2;
c.weightx=100;
c.weighty=0;
c.anchor=GridBagConstraints.WEST;
c.fill=GridBagConstraints.HORIZONTAL;
JTextField title = new JTextField(10);
title.setMinimumSize(new Dimension(300,25));
this.add(title,c);
c.gridy=1;
c.gridheight=2;
c.ipadx=20;
c.ipady=5;
c.weighty=200;
JTextArea description = new JTextArea(5,2);
JScrollPane descriptionScroll= new JScrollPane(description);
descriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
descriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
this.add(descriptionScroll, c);
}
}
使用此代码
我改变了你的代码。你没有使用 c.ipadx
我正在添加这个字段,它应该可以工作。