How can I make my columns different sizes using Gr

2019-04-03 02:03发布

问题:

I'm using a GridLayout and my code is as follows:

int changingVar = 1;

JPanel panel = new JPanel(new GridLayout(changingVar, 2));
panel.add(new JButton("BUTTON1"));
panel.add(new JButton("BUTTON2"));

This looks like:

___________________________________________
| [      BUTTON1     ] [     BUTTON2     ] |
___________________________________________

which is two evenly sized columns. I would like to make it like this:

___________________________________________
| [          BUTTON1         ] [ BUTTON2 ] |
___________________________________________

in which one column takes up more of the panel space then the other. How do I do this with gridlayout? I am not oppose to using another layout as long as I can have a varying amount of rows and columns that are two different sizes.

Thanks

回答1:

If you want this effect then you need to utilize the GridBagLayout.

http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Have fun with that one =P

EDIT:

You can work around the problem by employing a mixture of FlowLayout and GridLayout to get a similar effect. However, this solution will become extremely tedious and messy as your layout complexities become bigger.