java设置标签的位置
我所有的标签都工作正常,但是userLabel[3]没有正确定位 无论我做什么,标签“颜色:”总是显示在帧上,x坐标为0,y坐标位于帧的一半
JLabel[] userLabel = new JLabel[4];
for(int p = 0; p < userLabel.length; p++){
userLabel[p] = new JLabel();
userLabel[p].setSize(100,50);
frameSetUp.add(userLabel[p]);
}
userLabel[0].setText("Width of Frame:");
userLabel[1].setText("Height of Frame:");
userLabel[2].setText("# OF Balls:");
userLabel[3].setText("Color:");
userLabel[0].setLocation(10,35);
userLabel[1].setLocation(10,85);
userLabel[2].setLocation(10,135);
userLabel[3].setLocation(0,0); //no matter what coordinates I change this too, it wont reposition
图片: [IMG]http://i41.tinypic.com/23jfo9l.png[/IMG] http://i41.tinypic.com/23jfo9l.png
# 1 楼答案
例如
将显示如下:
这段代码的美妙之处在于,如果你想添加另一个字段,比如一个线条厚度字段,并希望添加它,使其成为倒数第二个字段,那么对代码所需的唯一更改就是更改以下内容:
为此:
其结果是:
无需计算如何更改颜色标签或JTextField的位置,因为布局管理器会为您完成所有繁重的工作
# 2 楼答案
最终得到了答案尝试将JLabel数组的大小增加1并运行它会很好