自动创建JLabel应该在其自身之下 [英] Auto create JLabels should be under themselves

查看:43
本文介绍了自动创建JLabel应该在其自身之下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动创建jlabels的代码.

I have a code that automatically create jlabels .

我希望每个label应该排成一行,而不是在旁边!

I want that each label should be at a row, Not beside!

我使用以下代码:

            lbl = new JLabel[rows];
        for (int i = 0; i < rows; i++) {
            lbl[i] = new JLabel(arrayResultRow[i].toString()+ "\n" );
        }

但是\n不起作用!

推荐答案

Google并学习Java Swing布局管理器教程并开始阅读.

Google and study the Java Swing layout manager tutorial and start reading.

很可能要将JLabel添加到默认使用FlowLayout的JPanel中,并且需要将容器的布局更改为GridLayout或BoxLayout.

Likely you're adding the JLabels to a JPanel which uses FlowLayout by default, and you need to change the layout of the container to GridLayout or BoxLayout.

这是链接:布置组件.

// add JLabels to a JPanel that uses GridLayout set to have
// 1 column and "rows" number of rows.
JPanel labelHolder = new JPanel(new GridLayout(rows, 1);
lbl = new JLabel[rows];
for (int i = 0; i < rows; i++) {
  lbl[i] = new JLabel(arrayResultRow[i].toString());
  labelHolder.add(lbl[i]);
}

这篇关于自动创建JLabel应该在其自身之下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆