简体   繁体   English

在单个列中组织JButton?

[英]Organise JButtons in a single column?

I've never really looked into layouts before, and it seems very confusing to me with all the choices given. 我以前从来没有真正研究过布局,并且给我所有选择似乎使我感到困惑。 What I'm trying to do is position buttons in one column down the centre of my JPanel (and maybe in one place have 2 buttons in one row). 我想做的是将按钮放置在JPanel中心下方的一列中(也许在一处将2个按钮排成一行)。 My question is; 我的问题是; which layout manager should I use? 我应该使用哪个布局管理器?

What about BoxLayout with vertical orientation or GridLayout with one column and multiple rows? 垂直方向的BoxLayout或一列多行的GridLayout呢?

To have 2 buttons in one row place a sub panel with the 2 buttons the same way as simple buttons. 要在一行中放置2个按钮,请在子面板中放置2个按钮,方法与简单按钮相同。

You can do it with just a BoxLayout, then centring the components inside the layout. 您可以仅使用BoxLayout来实现,然后将组件居中在布局内。

pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS)); //Creates the layout

And then in a component's class: 然后在组件的类中:

this.setAlignmentX(Component.CENTER_ALIGNMENT); //Centres the button

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM