2

Here is my shortened version of my code:

JPanel card1 = new JPanel();

JLabel switch1 = new JLabel(new ImageIcon("switch1.jpg"));
switch1.setLocation(TOPSWITCH, LEFTSWITCH1);
JLabel switch2 = new JLabel(new ImageIcon("switch1.jpg"));
switch2.setLocation(TOPSWITCH, LEFTSWITCH2);
JLabel switch3 = new JLabel(new ImageIcon("switch1.jpg"));
switch3.setLocation(TOPSWITCH, LEFTSWITCH3);

card1.add(switch1);
card1.add(switch2);
card1.add(switch3);

card1.setBackground(Color.BLACK);

/* JButton goToRoom = new JButton("TEST");

goToRoom.setLocation(180, 270);

card1.add(goToRoom); */

card1 is added to a JFrame of sized using my_frame.setSize(400, 300);.

The above code works as expected. But when I uncomment the comment piece of code, the JButton appears to the right side of the images(JLabels) instead of appearing below the images. What could be the problem?


Additional information:

final static int TOPSWITCH   = 150;

final static int LEFTSWITCH1 = 65 ;
final static int LEFTSWITCH2 = 155;
final static int LEFTSWITCH3 = 245;

switch1.jpg is of dimensions 91 x 150


Note: I want to do this without adding more JPanels.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 1
    *"What could be the problem?"* a) Wanting to set explicit locations. b) Fighting the layout manager. - Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 11 '15 at 11:55
  • Manual positioning does not work with layout managers, and `JPanels` have a default layout manager (`FlowLayout`). A simple, but very shortsighted fix would be using `null` layout, but it's better to get used to using layout managers instead. [The visual guide to layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) is a helpful resource for picking the right one. – kiheru Jun 11 '15 at 11:56
  • @kiheru *"..for picking the right one."* Or 2. Or 5. It is very rare that I would use just a single layout in a GUI. Great results can be had (relatively easily) by combining layouts according to need. – Andrew Thompson Jun 11 '15 at 11:58
  • @AndrewThompson , In that case, I think i'll create a JPanel with GridLayout and add two JPanels (with JLabels and a JButton) in them with Flowlayout. Thanks for the help. Can you post an answer so that I can accept it? – Spikatrix Jun 11 '15 at 11:59
  • 1
    @AndrewThompson Agreed! Forgot to write that in the comment, but luckily you fixed that omission. – kiheru Jun 11 '15 at 11:59

1 Answers1

2

What could be the problem?

  1. Wanting to set explicit locations.
  2. Fighting the layout manager.

Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433