【问题标题】:GridBagLayout is displaying JTextField and JTextArea as short, vertical linesGridBagLayout 将 JTextField 和 JTextArea 显示为短的垂直线
【发布时间】: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


【解决方案1】:
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 我添加了这个字段,它应该可以工作。

【讨论】:

    【解决方案2】:

    这是因为你没有给它长度。如果你在其中添加长度它会正常工作。用这个替换你的代码

    JTextField title = new JTextField(10);
    
    JTextArea description = new JTextArea(2,10);
    

    【讨论】:

    • 问题是 GridBagLayout(包含上面代码的面板)将 JPanel 压缩成一个小方块(它没有设置任何调整大小的权重)。
    • 1-,这个答案是在 3 小时前给出的。无需添加重复的答案。
    【解决方案3】:
    // ..
    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!
    // ..    
    
    1. Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。)

    【讨论】:

    • 我能够删除 setMinimumSize,现在 GridBag 管理组件的大小(我希望在调整窗口大小时扩展文本输入区域,但大小不必精确)。
    【解决方案4】:

    好的,问题不在于 JPanel 本身 - 封闭的 JPanel 有 Gridbag,但没有设置重量或任何尺寸。猜猜它表明问题出在你最不期望的地方。

    【讨论】:

    • 它还向您展示了为文本组件提供大小提示的重要性(如 setColumnssetRows 或通过构造函数)
    • “它表明问题出在你最不期望的地方” 我们更喜欢人们发布 MCVE 的众多原因之一(最少完整可验证示例)或SSCCE(简短、独立、正确的示例)。
    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2020-05-14
    相关资源
    最近更新 更多