我知道不推薦絕對定位,但我需要隨機散佈我的標籤以及隨機更改其位置。 我研究過如何使用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