1

I am trying to make a board game, I have Player 1 label and player 2 label set to the east and west. I am having trouble centre my 3 x 10 buttons. And is there any simpler way of making all 30 buttons at once? Below is my Code:

    JPanel panel = new JPanel();
    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(button3);panel.add(button4);
    panel.add(button5);panel.add(button6);panel.add(button7);panel.add(button8);
    panel.add(button9);panel.add(button10);panel.add(button11);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);
    frame.add(panel);

    Panel p = new Panel();
    p.setLayout(new BorderLayout());
    p.add(new Button("Throw dice"), BorderLayout.SOUTH);
    //p.add(new Button("dice Draw"), BorderLayout.SOUTH);
    p.add(new Label("Player 1"), BorderLayout.EAST);
    p.add(new Label("Player 2"), BorderLayout.WEST);
    frame.add(p);


    panel.setLayout(new GridLayout(3,10));
    panel.setSize(new Dimension(500, 200));
    frame.setSize(new Dimension(600, 300));


  }

}
0

2 Answers 2

6

you can make arrays of JButtons and edit them simply by creating a table of JButtons, and use them with a GridLayout cenetred in Borderlayout.CENTER :

JButton [] buttons = new JButton[n];
for(int i=0;i<n;i++){
    buttons[i] = new JButton("label "+ i);
    buttons[i].set...
    buttons[i].set...
    gridlayout.add(buttons[i]);
}

borderlayout.add(gridlayout, BorderLayout.CENTER);
panel.setLayout(borderlayout);

hope this helps.

0
1

A way to do it is:

panel.setLayout(new GridLayout(3,10));
panel.setSize(new Dimension(500, 200));
//Add this line
panel.setLocation((frame.getWidth()-panel.getWidth())/2, 0); // 0 is just the Y location
frame.setSize(new Dimension(600, 300));

Look into relative layout to manage all your layouts https://stackoverflow.com/a/5346794/643500

Keep in mind that you want all pieces to layout nicely with each other, otherwise issues can be a pain to deal with. Dahmad Boutfounast's solution is one of those nice ones to have.

And definitely use a list\array to manage all those.

2
  • Great it works :). I have two buttons added in south. But the second button is overlaying the first. Any idea how to sort the size of the button? p.add(new Button("Throw Stick"), BorderLayout.SOUTH); p.add(new Button("Stick Draw"), BorderLayout.SOUTH);
    – Umzz Mo
    Apr 10, 2012 at 17:41
  • The way I have it is not the best way to have. That is why I mentioned to follow Dahmad. When you have many layouts you have to think how they work together. Otherwise all you will be doing is workarounds. Give Dahmad's approach another try. Add those two lines to his solution GridLayout gridlayout = new GridLayout(); BorderLayout borderlayout = new BorderLayout();
    – Sully
    Apr 10, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.