Java swing GUI绝对定位

Java swing GUI绝对定位,java,swing,user-interface,setbounds,Java,Swing,User Interface,Setbounds,我知道不建议绝对定位,但我需要显示随机分散的标签以及随机改变其位置。 我已经研究过如何使用挫折,但它似乎不起作用。下面的代码显示流布局中的标签,当我使用setLayout(null)时,它显示一个空白框 public class GUI extends JFrame{ device mobiles[]; device station; JPanel pane= new JPanel(); public GUI() { setTitle("communication is Key");

我知道不建议绝对定位,但我需要显示随机分散的标签以及随机改变其位置。 我已经研究过如何使用挫折,但它似乎不起作用。下面的代码显示流布局中的标签,当我使用setLayout(null)时,它显示一个空白框

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

如果您能帮助我们找出问题所在,我们将不胜感激

下面的代码显示流布局中的标签,当我使用setLayout(null)时,它显示一个空白框

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);
}
布局管理器设置构件的大小和位置

如果不使用布局管理器,则负责设置每个组件的
大小
位置

通常,我会将大小设置为等于组件
首选大小

另外,您是否显示了随机生成的x/y值?可能这些值大于面板的大小

当我使用setLayout(null)时,它会显示一个空白框

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

什么布局设置为空?框架的面板。您的代码不使用上述方法。张贴导致问题的代码。我们不想猜测您可能在做什么或不在做什么。

多亏了@CasparNoree。。。建议的答案是从一开始就初始化Japnel:

JPanel pane = new JPanel(null);

将布局设置为null时,可以使用坐标手动设置边界

JFrame jf = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton();

// this is where you make it so setting the bounds actually does something
p.setLayout(null);
jb.setBounds(100,100,100,100);
p.add(jb);
jf.add(p);
jf.setVisible(true);

我尝试了pane.setLayout(null)和/或此.setLayout(null)。。。仍然播下一个空的画面。随机函数在帧大小范围内由最小值和最大值设置。@MNS,
而随机函数在帧大小范围内由最小值和最大值设置
-其中是执行此操作的代码。在你的回答中再次提及我的评论。除非你真的发布了你使用的代码,否则我们无法判断你所做的是否正确。别让我们猜!嗯,是的,在将组件添加到面板之前,需要设置布局。这就是为什么我建议在发布代码时发布实际测试的代码。我们需要看到你在做什么,而不是你说你在做什么,因为很多时候口头描述并不准确地反映你的真实代码。“但我需要显示我的标签随机分散以及随机改变它们的位置”-有一个布局管理器用于此-和