我知道不推荐绝对定位,但我需要随机散布我的标签以及随机更改其位置。 我研究过如何使用setBounds,但它似乎不起作用。下面的代码显示了流布局中的标签,当我使用setLayout(null)时,它显示一个空白帧。Java swing GUI绝对定位
public class GUI extends JFrame{
device mobiles[];
device station;
JPanel pane= new JPanel();
public GUI()
{
setTitle("communication is Key");
setSize(1000, 1000);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane.setBackground(Color.WHITE);
int x=0; int y=0;
mobiles= new device[10];
for (int i = 0; i < 10; i++) {
x=randInt();
y=randInt();
mobiles[i]= new device(1,x,y);
pane.add(mobiles[i]);
}
x=randInt();
y=randInt();
station = new device(0,x,y);
pane.add(station);
this.add(pane);
}
,这是类 “设备” 是扩展JLabel
public class device extends JLabel{
ImageIcon mob = new ImageIcon("mob.png");
ImageIcon tow = new ImageIcon("tower.png");
public device(int num, int x, int y)
{ if(num==1)
this.setIcon(mob);
else this.setIcon(tow);
this.setBounds(x, y, 3, 7);
}
}
在找出问题是什么任何帮助,将不胜感激。
*“但我需要证明我的标签随机分布以及随机改变自己的立场” * - 有一个布局管理器 - [例如](http://stackoverflow.com/questions/27974966/moving-jpasswordfield-to-absolute-position/27975101#27975101)和[示例](http://stackoverflow.com/examples/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601)#11822601) – MadProgrammer
[Example](http://stackoverflow.com/questions/17847816/position-image-在任何屏幕分辨率/ 17848635#17848635) – MadProgrammer