【问题标题】:How to prevent JScrollPane grow by its JTextArea without make it fixed size?如何防止 JScrollPane 通过其 JTextArea 增长而不使其固定大小?
【发布时间】:2020-10-10 21:19:30
【问题描述】:

我正在尝试使用 TabbedPane 编写公寓管理程序,我创建了一个使用 GroupLayout 扩展 JPanel 的类并将其添加到我的 TabbedPane。我在这个类中有两个 JTextArea,我把它们放在 JScrollPanes 中。

当我把它们写长的时候,它们的 ScrollPanes 会横向增长,我该如何防止呢?

我尝试添加textarea.setLineWrap(true); 行,它解决了我的问题,但它产生了一个新问题;我无法自动调整它们的大小我的 ScrollPanes。所以它们的大小是固定的。

    JTextArea diger = new JTextArea();
    JScrollPane digerS = new JScrollPane(diger);
    JTextArea rapor = new JTextArea();
    JScrollPane raporS = new JScrollPane(rapor);

    layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGap(5)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(kisiBilgileri)
                    .addComponent(daireBilgileri)
                    .addComponent(iletisimBilgileri)
                    .addComponent(_diger, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(digerS, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE) //textarea1's scrollpane
            )
            .addGap(5)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(ara, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(daireSec, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(kaydet, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(sil, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(_rapor, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
                    .addComponent(raporS, 0, GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE) //textarea2's scrollpane
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(aidatAy)
                            .addGap(5)
                            .addComponent(aidatEvDurumu)
                    )
                    .addComponent(aidatTuru)
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(aidatMiktar)
                            .addGap(5)
                            .addComponent(aidatOde)
                    )
            )
            .addGap(5)
    );

我的程序看起来像:

【问题讨论】:

  • 我们需要一个minimal reproducible example 以便我们自己重现这个。我无法从您向我们展示的代码中确定您是将 JScrollPanes 添加到布局中,还是直接添加 JTextAreas。
  • textfield.setLineWrap(true); - 为什么将 JTextArea 称为“textField”?这很令人困惑,因为还有一个 JTextField 组件。使用有意义的名称使您的代码可读。 我的 ScrollPanes 无法自动调整它们的大小 - 不知道这意味着什么。
  • @camickr 抱歉,打错了:)
  • @VGR 现在怎么样了?
  • 当您创建 JTextArea 时,您应该使用类似:new JTextArea(5, 20),当数字“建议”文本区域的默认行/列时,它可以确定其首选大小。布局管理器可能会覆盖建议,但至少它有一个起点。我从未使用过 GroupLayout,所以我不知道所有这些约束是什么。

标签: java swing jscrollpane jtextarea grouplayout


【解决方案1】:
JTextArea diger = new JTextArea();

当您创建像上面这样的 JTextArea 时,文本区域不知道如何调整自身大小,因此在添加/删除文本时它的大小可能会发生变化(取决于使用的布局管理器,我不知道 GroupLayout 是如何工作的) .

你应该使用类似的东西:

JTextArea diger = new JTextArea(5, 20);

建议文本区域的行数和列数。现在文本区域可以确定自己的首选大小,布局管理器可以使用此信息。

注意,同样适用于 JTextField,除了您只能指定列。

【讨论】:

    猜你喜欢
    • 2017-03-31
    • 2018-09-30
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2017-08-08
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多