Results 1 to 2 of 2
  1. #1
    umzz is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default How do i centre by grid of buttons?

    Hi there,

    I am making a board game called Senet, and I need to make a 3x10 grid. At the moment my frame is covered up with the grid buttons and when i maximize the window it becomes stretched out all the way through. I dont want this to happen, I just want to have my 3x10 grid of buttons in the centre of my frame and have empty spaces all around it so that i can add more things to it. Below is my code:

    public void gui()
    {
    JFrame frame = new JFrame("Games");
    frame.setVisible(true);
    frame.setSize(600,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3,10));


    JButton button1 = new JButton();JButton button2 = new JButton();
    JButton button3 = new JButton();JButton button4 = new JButton();
    JButton button5 = new JButton();JButton button6 = new JButton();
    JButton button7 = new JButton();JButton button8 = new JButton();
    JButton button9 = new JButton();JButton button10 = new JButton();
    JButton button11 = new JButton();JButton button12 = new JButton();
    JButton button13 = new JButton();JButton button14 = new JButton();
    JButton button15 = new JButton();JButton button16 = new JButton();
    JButton button17 = new JButton();JButton button18 = new JButton();
    JButton button19 = new JButton();JButton button20 = new JButton();
    JButton button21 = new JButton();JButton button22 = new JButton();
    JButton button23 = new JButton();JButton button24 = new JButton();
    JButton button25 = new JButton();JButton button26 = new JButton();
    JButton button27 = new JButton();JButton button28 = new JButton();
    JButton button29 = new JButton();JButton button30 = new JButton();

    panel.add(button1);panel.add(button2);panel.add(bu tton3);panel.add(button4);
    panel.add(button5);panel.add(button6);panel.add(bu tton7);panel.add(button8);
    panel.add(button9);panel.add(button10);panel.add(b utton11);panel.add(button12);
    panel.add(button13);panel.add(button14);panel.add( button15);panel.add(button16);
    panel.add(button17);panel.add(button18);panel.add( button19);panel.add(button20);
    panel.add(button21);panel.add(button22);panel.add( button23);panel.add(button24);
    panel.add(button25);panel.add(button26);panel.add( button27);panel.add(button28);
    panel.add(button29);panel.add(button30);

    panel.setComponentOrientation(ComponentOrientation .LEFT_TO_RIGHT);
    frame.add(panel);

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

    Default Re: How do i centre by grid of buttons?

    You'll want to place the JButtons in their own JPanel, one that uses GridLayout, and then place that JPanel in another one that uses its own layout. Which layout? it will depend on the needs of your application. I suggest that you read the layout tutorials which start here: Laying out Components in a Container

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2012, 07:43 PM
  2. 2D grid
    By ranss41 in forum New To Java
    Replies: 0
    Last Post: 01-06-2012, 04:41 PM
  3. Son of Grid Engine 8.0.0d
    By java software in forum Java Software
    Replies: 0
    Last Post: 12-14-2011, 05:45 PM
  4. Help with resizing Buttons in a grid
    By satanfx55 in forum AWT / Swing
    Replies: 6
    Last Post: 05-14-2011, 08:26 PM
  5. Rotating Rectangle about its Centre
    By Lance in forum Java 2D
    Replies: 6
    Last Post: 02-28-2011, 11:28 PM

Tags for this Thread

Posting Permissions

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