java 如何将 JFrame 中的按钮居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10093122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:35:01  来源:igfitidea点击:

How to centre the Buttons in my JFrame?

javaswingjpaneljbuttonborder-layout

提问by Umzz Mo

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:

我正在尝试制作棋盘游戏,我将玩家 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));


  }

}

回答by Ahmed Laatabi

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

你可以让阵列JButtons简单地通过创建一个表,并对其进行编辑JButtons,并与使用它们GridLayout在cenetred 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.

希望这可以帮助。

回答by Hitham S. AlQadheeb

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

查看相对布局以管理所有布局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.

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

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

并且绝对使用列表\数组来管理所有这些。