Results 1 to 7 of 7
  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default center button on JPanel? help please

    so as the title says i'm stuck... :(

    i just can't seem to fit my buttons around the mid of my panel...

    in short:
    i'm making a game like minesweaper..
    so my first JFrame is a frame with one JPanel with four buttons on it... (on for each difficulty)
    making the frame and panel isn't a problem, but i would like to have my 4 buttons centered i tried using a BoxLayout, but the my buttons are sticked to the top of the panel.. :(

    i would really appreciate it if someone could help me

    grtz liluma

    this is the code i have so far...

    Java Code:
    package gui;
    
    import java.awt.Component;
    
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    import domein.DomeinController;
    
    public class DifficultyPanelChooser extends JPanel
    {
    	/**
    	 * serialversion key
    	 */
    	private static final long serialVersionUID = 5388440409753599885L;
    	
    	//attributes
    	private DomeinController domCntrl;
    	private JButton easy, medium, hard, insane;
    	
    	//constructor
    	public DifficultyPanelChooser(DomeinController domCntrl)
    	{
    		super();
    		setDomeinController(domCntrl);
    		initGui();
    	}
    	
    	private void initGui()
    	{
    		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    		easy = new JButton("easy");
    		medium = new JButton("medium");
    		hard = new JButton("hard");
    		insane = new JButton("insane");
    		
    		//centering the buttons
    		easy.setAlignmentX(Component.CENTER_ALIGNMENT);
    		easy.setAlignmentY(Component.CENTER_ALIGNMENT);
    		medium.setAlignmentX(Component.CENTER_ALIGNMENT);
    		medium.setAlignmentY(Component.CENTER_ALIGNMENT);
    		hard.setAlignmentX(Component.CENTER_ALIGNMENT);
    		hard.setAlignmentY(Component.CENTER_ALIGNMENT);
    		insane.setAlignmentX(Component.CENTER_ALIGNMENT);
    		insane.setAlignmentY(Component.CENTER_ALIGNMENT);
    		
    		
    		add(easy);
    		add(medium);
    		add(hard);
    		add(insane);
    	}
    	
    	//setter voor domeinController
    	private void setDomeinController(DomeinController domCntrl)
    	{
    		if(domCntrl != null)
    			this.domCntrl = domCntrl;
    		else
    			throw new NullPointerException("failed to initiate DomeinController");
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: center button on JPanel? help please

    Put the JButtons into another JPanel that uses GridLayout (2, 2) or (1, 4) or (4, 1) however you want to place them, and then place that JPanel into the main JPanel using GridBagLayout but not using GridBagConstraints. This will center your inner JPanel in the outer JPanel.

  3. #3
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: center button on JPanel? help please

    ty fubarable. ^^

    one more question.
    is it possible to have some more vertical space amongst the different buttons? say 10 pixels orso?

  4. #4
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: center button on JPanel? help please

    panel.add(exitButton,getConstraints(0,0,1,1, GridBagConstraints.SOUTH)); WEST, EAST, CENTER(MIDDLE)

    might be of some help too you

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: center button on JPanel? help please

    is it possible to have some more vertical space amongst the different buttons? say 10 pixels orso?
    There is a GridLayout constructor that lets you specify the spacing.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: center button on JPanel? help please

    No need to set the layout. The default layout for a JPanel is a FlowLayout with center alignment.

  7. #7
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default Re: center button on JPanel? help please

    ty all. :p ^^
    works great.

Similar Threads

  1. Button in a jpanel
    By AARON22 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2011, 03:38 PM
  2. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  3. Replies: 4
    Last Post: 09-25-2010, 09:03 AM
  4. trying to animate jpanel with button
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2009, 12:02 AM
  5. Move Jpanel to center
    By tpyq in forum NetBeans
    Replies: 1
    Last Post: 12-15-2008, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •