1

I have a JWindow which has a JLabel and JButton in a JPanel. I'm trying to set the location of the JButton to the center. For some reason, setting the y location works, but the x location doesn't change. What is wrong with my code?

JWindow window = new JWindow();
    JPanel panel = new JPanel();
    panel.setLayout(new OverlayLayout(panel));
    JButton singlePlayer = new JButton("Single Player", new ImageIcon(this.getClass().getResource("ButtonSword.png")));
    singlePlayer.setPreferredSize(new Dimension(170, 50));
    singlePlayer.setLocation(window.getWidth()/2-85, window.getHeight()/2-25);
    JLabel label = new JLabel(new ImageIcon(this.getClass().getResource("Splash.png")));
    panel.add(singlePlayer);
    panel.add(label);
    window.getContentPane().add(panel);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
bobbobbob
  • 92
  • 1
  • 12
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Jan 01 '15 at 01:36
  • .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. 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). 4) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 01 '15 at 01:37
  • Use a GridBagLayout, this centres components by default – MadProgrammer Jan 01 '15 at 01:55

1 Answers1

1

If you want to play with the OverlayLayout, then you will also need to make sure that you use:

setAlignmentX(Component.CENTER_ALIGNMENT);
setAlignmentY(Component.CENTER_ALIGNMENT);

for both the label and the player so that the alignments are in sync.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • what if I wanted to make the button 75 pixels lower? – bobbobbob Jan 01 '15 at 20:16
  • The OverlayLayout is not that flexible. I suggest you try using a different layout manager. For example you can set the layout manager of your JLabel and then add the JButton to the label. No need to use the OverlayLayout. – camickr Jan 01 '15 at 22:00
  • Which layout manager? I'm using overlay layout because I need to put the button over the label. – bobbobbob Jan 03 '15 at 01:16
  • And I suggested you could set the layout manager for the label and then add the button to the label. Start with a FlowLayout. It won't do what you want but it will demonstrate the concept. Then you try different layout manager until you get the layout you want. – camickr Jan 03 '15 at 01:44