Establecer componente en el centro de la página

How can I set a component (say button) at center of the panel?

solía Flowlayout with layout constraint as center, but I am getting button at centro Superior position of the panel.

preguntado el 01 de febrero de 12 a las 04:02

3 Respuestas

This can be achieved using either GridBagLayout as mentioned by AVD1 or BoxLayout. Ver esta respuesta para código de muestra.

Personally I'd use GBL for this because fewer lines of code are required to get the component laid out & on-screen (centered in the parent container).

  1. I do not understand why that answer is not getting more up-votes, but that aside..

contestado el 23 de mayo de 17 a las 14:05

@AVD It's one of my favorite answers. Especially because of the pretty screen-shots supplied by trashgod (I often borrow them for illustrating points in other threads). Really rounds it out nicely. :) - Andrew Thompson

Utilizan GridBagLayout instead of FlowLayout.

JPanel panel=new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JButton("Sample")); // will use default value of GridBagConstraints

Respondido 01 Feb 12, 08:02

The first 2 lines can be written as one. +1 (one of the few things for which I use GBL.) - Andrew Thompson

Utilizar null layout and set the button's bounds, like this:

// assuming you're extending JPanel
private JButton button; // data field


this.addComponentListener(new ComponentAdapter(){
   public void componentResized(ComponentEvent e){

private void setButtonBounds(){
   int bw = 90; // button width
   int bh = 30; // button height
   int w = getWidth();
   int h = getHeight();
   button.setBounds((w - bw) / 2, (h - bh) / 2, bw, bh);

If you start getting many buttons or a complex layout, consider using MigLayout.

Respondido 01 Feb 12, 08:02

-1 for even suggesting a null layout. Edit that out & I will remove the down-vote (but I am not giving an up-vote for MigLayout, when there are 2 J2SE layouts that can do what is required). - Andrew Thompson

Nunca he entendido realmente por qué null layout is so bad, but fair enough. I'll leave my answer and accept the -1. - rtheunissen

Consulte nuestra página setLayout (nulo) nunca es necesario. ¡Siempre! for my comments (some uncharitable people might call it a 'rant') on the subject. - Andrew Thompson

Sure, but why not encapsulate that logic into a layout (wherein it becomes unnecessary to call setLayout(null)? Even your example above could (both be improved and) be better encapsulated in a layout. It seems to make sense to do it inline in the code until you require a 2nd (3rd ..17th) instance of 'center a component', which is a pretty basic requirement BTW. As to the comment that it can 'be improved'. Why hard-code the widths of the button when there is getPreferredSize() (which takes into account the current button text, borders, PLAF,..)? Almost a classic e.g. of my point! - Andrew Thompson

+1 for making good sense, and I actually do agree with you. It does seem redundant to nullify the layout and set the bounds manually. I think it comes down to a lack of understanding of how the core layout managers actually function. So I agree that a manager / encapsulation is better, but they would still both be right answers and I wouldn't -1 someone for suggesting an appropriate (perhaps silly) use of a null layout. Again, I don't think this is the place for this discussion, so I'll end things there. Thanks though. - rtheunissen

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.