0

I know that absolute positioning is not recommended, but I need to show my labels randomly scattered as well as randomly changing their positions. I have researched how to use setBounds but it doesn't seem to work. The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.

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

and this is class "devices" that extends 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);
}

}

any help in finding out what the problem is, would be be appreciated.

MNS
  • 45
  • 2
  • 2
  • 8
  • *"but I need to show my labels randomly scattered as well as randomly changing their positions"* - There's a layout manager for that - [for example](http://stackoverflow.com/questions/27974966/moving-jpasswordfield-to-absolute-position/27975101#27975101) and [example](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) – MadProgrammer Apr 19 '17 at 22:37
  • [Example](http://stackoverflow.com/questions/17847816/position-image-in-any-screen-resolution/17848635#17848635) – MadProgrammer Apr 19 '17 at 22:39

3 Answers3

1

The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.

The layout manager sets the size and location of the component.

If you don't use the layout manager, then you are responsible for set the size and location of each component.

Typically I would set the size to equal to the components preferred size.

Also, did you display the x/y value that are randomly generated? Maybe the values are larger than the size of the panel.

and when I use setLayout(null) it shows a blank frame.

What layout is set to null? The panel of the frame. Your code doesn't use the above method. Post the code that you use to cause the problem. We don't want to guess what you may or may not be doing.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I tried pane.setLayout(null) and/or this.setLayout(null)... still sowed an empty frame. and the random function is set by min and max within the range of the frame size. – MNS Apr 19 '17 at 21:26
  • @MNS, `and the random function is set by min and max within the range of the frame size` - where is the code that does that. Again refer to my comment in your answer. Unless you actually post the code you use, we can't tell if what you are doing is correct. Don't make us guess! – camickr Apr 19 '17 at 21:52
0

thanks to @CasparNoree ... the answer suggested was to initialize the Japnel from the start:

JPanel pane = new JPanel(null);
MNS
  • 45
  • 2
  • 2
  • 8
  • Well, yes the layout needs to be set before you add components to the panel. That is why I suggested when you post code you post the code you are actually testing with. We need to see what you are doing, not what you say you are doing because many times the verbal description does not accurately reflect your real code. – camickr Apr 19 '17 at 21:50
0

When you set the layout to null you can set the bounds manually with coordinates.

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);
Community
  • 1
  • 1
Joe Giusti
  • 159
  • 1
  • 3