Layout Swing alignement

Asked by At

I need to create a layout like this:

------------------------------
|                             |
|               line1(right)  |
|  
|line2(left)                   
|
|
|  line3
|
|
|
|
------------------------------

where line3 is more than a line - it's a matrix of buttons. The size of the matrix needs to be fixed not like in GridLayout.

How to do that?


I came with a question:How to put the button on the front line?(Now he is in the middle)

Container contentPane = this.getContentPane();
contentPane.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
JButton button = new JButton("Button 1");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
contentPane.add(button, c);

1 Answers

0
camickr On

A GridbagLayout should work:

For lines 1 and 2 you would need to specify the "anchor" constraint for each component you add

where line3 is more than a line - it's a matrix of buttons

Then you would use a JPanel with whatever layout is appropriate for the buttons.

Read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.