Auto create JLabels should be under themselves

允我心安 提交于 2020-01-05 04:54:08

问题


I have a code that automatically create jlabels .

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

I use this code:

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

But \n does not work!


回答1:


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

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.

Edit: here's the link: Laying out Components.

i.e.,

// 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]);
}


来源:https://stackoverflow.com/questions/17710563/auto-create-jlabels-should-be-under-themselves

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!