设置JTextArea(Java Swing)的尺寸(行数)
下面是一个愚蠢的数据输入应用程序的代码。我希望“oggetto”JTextArea具有一定的大小,以便用户可以输入不同的信息行:设置JTextArea(Java Swing)的尺寸(行数),java,swing,Java,Swing,下面是一个愚蠢的数据输入应用程序的代码。我希望“oggetto”JTextArea具有一定的大小,以便用户可以输入不同的信息行: JPanel body = new JPanel(); body.setLayout(new GridLayout(10, 1)); body.add(new JLabel("Inserimento di un nuovo protocollo")); JTextArea oggetto = new JTextArea(5,1); oggetto.setOpaque
JPanel body = new JPanel();
body.setLayout(new GridLayout(10, 1));
body.add(new JLabel("Inserimento di un nuovo protocollo"));
JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
//oggetto.setSize(100, 100);
...
body.add(oggetto);
jframe.add(body);
jframe.setVisible(true);
我已经尝试了很多方法,但当我启动应用程序时,我只得到了一条独特的线的“oggetto”jtextarea。。。
你能帮我吗?我建议你使用另一种适合这里的布局,如或 使用
GridBagLayout
可以沿水平和垂直方向给出组件大小的百分比
阅读更多关于
注意:切勿为组件调用setSize()
,并将其留给布局管理器来决定组件的大小和位置
阅读更多
示例代码(使用
GridBagLayout
)
问题在于您选择的布局。因为将组件划分为大小相等的块: GridLayout类是一个布局管理器,用于布局容器的 矩形网格中的组件。容器分为两部分 大小相等的矩形,每个矩形中放置一个组件 你应该使用更多的灵活性
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 200));
JPanel body = new JPanel();
body.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 1.0;// 100%
c1.gridx = 0;// column 0
c1.gridy = 0;// row 0
body.add(new JLabel("Inserimento di un nuovo protocollo"), c1);
JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.weightx = 1.0; // 100%
c2.gridx = 0; // column 0
c2.gridy = 1; // row 1
body.add(oggetto, c2);
frame.add(body);
frame.setVisible(true);
}
我建议使用它,因为它是理解和实现的最佳布局
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 200));
JPanel body = new JPanel();
double[][] dim = {
{20, TableLayout.FILL, 20}, // column widths: 20 distance right/left, TableLayout.FILL usable place
{20, 17, 5, TableLayout.FILL, 20} // row height: 20 distance up/down, 17 place for label, 5 distance between label-textarea
};
body.setLayout(new TableLayout(dim));
JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
body.add(new JLabel("Inserimento di un nuovo protocollo"), "1,1"); // col: 1, row: 1
body.add(oggetto, "1,3");// col: 1(TableLayout.FILL), row: 3 (TableLayout.FILL)
frame.add(body);
frame.setVisible(true);
}
要添加更多组件,请定义“dim”,如本例所示:
double[][] dim = {
{20, TableLayout.FILL, 20},
{20, 17, 5, TableLayout.FILL /*label1,distance,component1*/, 17, 5, TableLayout.FILL/*label2,distance,component2 etc...*/, 20}
};
请解释一下。不清楚您想尝试什么?抱歉…我的意思是我希望oggetto JTextArea具有一定的高度…以便用户可以输入和读取两行或更多行数据。现在它只显示一行,如下图所示:
oggetto
的大小将与添加到GridLayout
的最大组件的大小相同,因为它使所有组件的大小相同。为了更快地获得更好的帮助,请发布一个(最小的、完整的、可验证的示例)。为什么,您的建议和您在这段代码中描述的不一致?@LoryLory请看我的TableLayout示例。@dit谢谢,我想我已经成功了。我还有一个问题要问你:我如何才能让所有的单词都包含在文本区域内?我的意思是,除非我输入ENTER键,否则单词会一直被写在一行中,它们被隐藏在文本区域的边界处。我希望当到达文本区域的边界时,应该开始一个新行。你能帮我吗?@LoryLory只用oggetto.setLineWrap(真)@dit我已经熟悉了TableLayout,现在我需要知道以下几点:如果我只知道运行时的列数或行数,如何初始化数组double[][]dim?例如,我需要打印一个对象、书籍的列表。我只需要一列,行数和书本大小的列表一样多。我该怎么办?
double[][] dim = {
{20, TableLayout.FILL, 20},
{20, 17, 5, TableLayout.FILL /*label1,distance,component1*/, 17, 5, TableLayout.FILL/*label2,distance,component2 etc...*/, 20}
};