【发布时间】: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