How to centre the Buttons in my JFrame?

被刻印的时光 ゝ 提交于 2020-01-03 02:26:08

问题


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));


  }

}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10093122/how-to-centre-the-buttons-in-my-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!