如何在我的JFrame中居中按钮?

时间:2012-04-10 17:01:39

标签: java swing jpanel jbutton border-layout

我正在尝试制作棋盘游戏,我在东部和西部设置了玩家1标签和玩家2标签。我无法将3 x 10按钮放在中心位置。有没有更简单的方法一次制作所有30个按钮?以下是我的代码:

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


  }

}

2 个答案:

答案 0 :(得分:6)

您可以创建JButtons数组,只需创建一个JButtons表格,然后在GridLayout中使用Borderlayout.CENTER cenetred来编辑它们:

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

希望这会有所帮助。

答案 1 :(得分:1)

一种方法是:

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

查看相对布局以管理所有布局https://stackoverflow.com/a/5346794/643500

请记住,您希望所有部分能够很好地相互布局,否则问题可能会很难处理。 Dahmad Boutfounast的解决方案是其中一个很好的解决方案。

绝对使用list \ array来管理所有这些。