java - Java Swing:JTextArea 填充整个分配的空间
问题描述
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import static java.awt.GridBagConstraints.BOTH;
public class mwe extends JFrame {
private JPanel x, y, z, u,v,w,jtext;
private class MyBorderedPanel extends JPanel {
MyBorderedPanel( String title ) {
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title
));
}
}
private class MyTextPanel extends JPanel {
JTextArea textArea;
MyTextPanel( String title ) {
textArea= new JTextArea();
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
title));
this.setLayout(new BorderLayout());
JScrollPane pane= new JScrollPane();
pane.add(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
this.add(pane);
}
}
public mwe() {
x= new MyBorderedPanel("x");
y= new MyBorderedPanel("y");
z= new MyBorderedPanel("z");
u= new MyBorderedPanel("u");
v= new MyBorderedPanel("v");
w= new MyBorderedPanel("w");
jtext= new MyTextPanel("textArea");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
manageLayout();
}
private void manageLayout() {
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = constraints.weighty = 13.00;
constraints.gridwidth = 1;
constraints.gridheight = 4;
this.add(x, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = constraints.weighty = 8.00;
constraints.gridwidth = 3;
constraints.gridheight = 1;
this.add(y, constraints);
{ //adding dummes: 2x2, 1x1, 1x1
constraints.gridx = 1;
constraints.gridy = 1;
constraints.weightx = constraints.weighty = 2.00;
constraints.gridwidth = 1;
constraints.gridheight = 2;
this.add(u, constraints);
constraints.gridx = 2;
constraints.gridy = 1;
constraints.weightx = constraints.weighty = 1.00;
constraints.gridwidth = 1;
constraints.gridheight = 1;
this.add(v, constraints);
constraints.gridx = 2;
constraints.gridy = 2;
constraints.weightx = constraints.weighty = 1.00;
constraints.gridwidth = 1;
constraints.gridheight = 1;
this.add(v, constraints);
}
constraints.gridx = 3;
constraints.gridy = 1;
constraints.weightx = constraints.weighty = 5.00;
constraints.gridwidth = 1;
constraints.gridheight = 3;
this.add(jtext, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
constraints.weightx = constraints.weighty = 3.00;
constraints.gridwidth = 2;
constraints.gridheight = 1;
this.add(z, constraints);
}
public static void main( String ... args ) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame= null;
frame = new mwe();
frame.pack();
frame.setVisible(true);
}
});
}
}
我有上面的代码模拟了应用于 GridBagLayout() 的黄金比例的想法。输出如下:
现在,您可以看到 TextArea 是空的。我希望它的托管面板基本上充满了“白色”空间,即使此时文本区域是空的。我记得在 SE 读到过这BorderLayout
就是要走的路,所以我试了一下。因此,基本上,我希望文本区域将其托管面板填充到边缘,并且滚动窗格也是可见的。
如何做到这一点?可能上述 MWE 最相关的代码是 MyTextPanel 类。
解决方案
现在,您可以看到 TextArea 是空的。
这是因为您实际上并未将文本区域添加到滚动窗格中。
JScrollPane pane= new JScrollPane();
pane.add(textArea);
上面的代码是错误的。您不会将组件直接“添加”到 JScrollPane。
相反,您将组件添加JViewport
到JScrollPane
.
这是通过执行以下任一操作来完成的:
JScrollPane pane= new JScrollPane(textArea);
这会将文本区域添加到视口,或者
JScrollPane pane= new JScrollPane();
pane.setViewportView(textArea);
推荐阅读
- typescript - 在 Typescript 中创建类时出现重复标识符错误
- python - 在opencv python中限制扫描区域
- google-apps-script - 在 Google Apps 脚本线性优化服务中添加约束
- python - Python 在给定路径内浏览并获取所有扩展名为“.txt”的文件,但路径存储在字典中
- mysql - 在laravel中将多个值插入数据库
- python - 如何使用groupby获取与列最大值对应的所有行
- java - 如何在 NatTables 的一列中对各种数据类型进行排序
- json - 我在尝试将我的 json 文件分成块 amd 然后创建 sifferent csv 文件并最终合并为一个 csv 文件时遇到错误
- ios - 添加“use_frameworks!” 在 podfile 中
- postgresql - TimescaleDB 时间戳类型