1
import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@SuppressWarnings("serial")
public class ButtonLocationDemo extends JFrame implements ActionListener{
  /// Create variables
  private JButton button;
  private JButton button1;
  private JButton button2;
  private JButton button3;
  private JButton button4;
  // private BufferedImage image;

  public ButtonLocationDemo(){
    // button1.setForeground(Color.WHITE);

    // Add buttons
    JPanel p = new JPanel();
    button = new JButton("Doorgaan");
    button1 = new JButton("Opslaan");
    button2 = new JButton("Spelregels");
    button3 = new JButton("Naar hoofdmenu");
    button4 = new JButton("Afsluiten");

    button1.setAlignmentX(Component.CENTER_ALIGNMENT);

    button1.setBackground(Color.black);
    button1.setForeground(Color.white);
    button1.setBorder(null);
    button1.setBounds(250,150,100,50);

    button.setBounds(250,50,100,50);
    button.setBackground(Color.black);
    button.setForeground(Color.white);
    button.setBorder(null);

    button2.setBounds(250,250,100,50);
    button2.setBackground(Color.black);
    button2.setForeground(Color.white);
    button2.setBorder(null);

    button3.setBounds(250,350,150,50);
    button3.setBackground(Color.black);
    button3.setForeground(Color.white);
    button3.setBorder(null);

    button4.setBounds(250,450,100,50);
    button4.setBackground(Color.black);
    button4.setForeground(Color.white);
    button4.setBorder(null);

    // Add buttons
    p.add(button);
    p.add(button1);
    p.add(button2);
    p.add(button3);
    p.add(button4);
    p.setBackground(Color.black);
    p.setLayout(null);

    getContentPane().add(p);
    //setLayout(null);
    setDefaultCloseOperation(3);
    setSize(720,720);
    setVisible(true);

    // Add actionListeners
    button.addActionListener(this);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
  }

  public static void main(String[] args) {
    new ButtonLocationDemo();
  }

  // Action performer
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==this.button){
      System.out.println("Doorgaan");
    }
    if (e.getSource()==this.button1){
      System.out.println("Opslaan");    
    }
    if (e.getSource()==this.button2){
      System.out.println("Spelregels");  
    }
    if (e.getSource()==this.button3){
      System.out.println("Naar hoofdmenu");  
    }
    if (e.getSource()==this.button4){
      System.out.println("Afsluiten");  
      System.exit(0);  
    }    
  }
}

I tried using :"button1.setAlignmentX(Component.CENTER_ALIGNMENT);" but that didn't work out very well. Is there any way I can automatically center my buttons to the middle?

How it looks: https://i.stack.imgur.com/Krmhp.jpg

mKorbel
  • 109,525
  • 20
  • 134
  • 319
theButcher
  • 75
  • 1
  • 2
  • 11

3 Answers3

3

Use GridLayout

p.setLayout(new GridLayout(5,1));

and remove all the setBounds() methods calls.

Just handover it to layout manager to manage the position of the components. Don't use null Layout.


--EDIT--

you can try GridBagLayout also

p.setLayout(new GridBagLayout());
GridBagConstraints gc=new GridBagConstraints();

gc.gridx=0;
gc.weightx=0.4;
gc.insets=new Insets(5, 5, 5, 5);
//gc.anchor=GridBagConstants.NORTH;


// Add buttons
gc.gridy=0;
p.add(button,gc);
gc.gridy=1;
p.add(button1,gc);
gc.gridy=2;
p.add(button2,gc);
gc.gridy=3;
p.add(button3,gc);
gc.gridy=4;
p.add(button4,gc);
Braj
  • 46,415
  • 5
  • 60
  • 76
  • +1, do not hardcode the layout. Any numbers you think of, there's a legitimate configuration that will break it. Layout managers can do a pretty good job, I don't think I ever needed to manually set bounds, positions or alignment on any components I made – Ordous Apr 10 '14 at 18:01
  • I tried your first method it does the trick but makes the buttons HUGE. Then I tried your second method and it works like a charm! Many thanks! – theButcher Apr 10 '14 at 18:14
  • Great I know that's why I suggested second option. – Braj Apr 10 '14 at 18:16
  • One little question: If I would change the font to a little bigger one does it automatically scale? – theButcher Apr 10 '14 at 18:20
  • Read here [Setting the Default Font of Swing Program in Java](http://stackoverflow.com/questions/7434845/setting-the-default-font-of-swing-program-in-java) – Braj Apr 10 '14 at 18:26
  • Don't use new GridLayout(5, 1). Instead use `new GridLayout(0, 1)`, which means 1 column and any number of rows. When you specify both values, one value takes precedence. Of course, it would not be an issue if you have exactly 5 components, but if you ever change the code to have more/less it could become an issue. – camickr Apr 10 '14 at 19:44
0

You could use your panel size and buttons size like..

button1.setSize(100,50);
button1.setPosition(p.getSize().getWidth()/2 - button1.getSize().getWidth()/2, 150);
Arkillon
  • 51
  • 4
0

if you want to use setBounds method for alignment then using screenSize will be fine like here

Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    int width=screenSize.width;
    int height=screenSize.height;

and arrange the x and y co-ordinate like you want to do with reference to width and height of screen size. The Good programming should use layouts as Braj told.

Raju Sharma
  • 2,496
  • 3
  • 23
  • 41