The thing is pretty simple, but of course you have to search a little more yourself.
By the way, you have a problem with layout control and button size.
Please forget with any layout creator, because they generate confusion code, and it’s not good to understand what is really happening.
Usually for beginners I’m talking about a specific layout, that is, BoxLayout [edit: of course, there is a much simpler way to achieve the goal, this answer has a learning goal and wants to be big enough to be understood, and not just “here, make this copy” )
First of all, we need to know how the box layout works. It has two types of AXIS (this is how you added the components located on top) (PAGE_AXIS) and from left to right (LINE_AXIS). Using this two axes and a nested different layout, you can do whatever you want.
The first thing to know is to insert more layout.
To insert the layout, we will work with JPanel, each with a different axis, which will be placed one inside the other.
Begin your case study. You have an inscription above the three buttons in the center of the panel. We can start from the inside panel, with Components; we need PAGE_AXIS (top to botton), which will include:
// +---------------------+ // | white space | // | | // + - - - - - - - - - - + // |JLABEL JLABEL JLABEL| // + - - - - - - - - - - + // | white space | // |_____________________| // | button | // |---------------------| // |_____________________| // | button | // |---------------------| // |_____________________| // | button | // |---------------------| // | white space | // +---------------------+
As you can see, the panel is suitable for matching the width of the components, the trick is that BoxLayout prefers to give the component the maximum size. In this case, there are no fields, so we need an external other JPanel with LINE_AXIS to place the left and right fields, the result will be like this:
// +---+---------------------+---+ // | | white space | | // | | | | // | + - - - - - - - - - - + | // | W |JLABEL JLABEL JLABEL| W | // | H + - - - - - - - - - - + H | // | I | white space | I | // | T _____________________ T | // | E | button | E | // | --------------------- | // | S _____________________ S | // | P | button | P | // | A --------------------- A | // | C _____________________ C | // | E | button | E | // | --------------------- | // | | white space | | // +-----------------------------+
So, the first thing we need to know is a hot layout setting for each panel.
// Initializing JPanel outside = new JPanel(); JPanel inside = new JPanel(); // setting a layout with horizontal alignment outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS)); // setting a layout with vertical alignment inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));
After that we have to fill in the panel. Starting from the side. An outsider needs (looking at my image) first a horizontal space, then an inner panel, then another horizontal space. I keep adding them.
// create an horizontal space of 20px outside.add(Box.createHorizontalStrut(20)); outside.add(inside); outside.add(Box.createHorizontalStrut(20));
Now we go inside, we need a big white space, a shortcut, still white, a button, a little white, a button, a little white, a third button and a big white. I continue to fill the inner panel with this.
// create a vertical space of 20px inside.add(Box.createVerticalStrut(20)); JLabel title = new JLabel("THE TITLE"); inside.add(title); inside.add(Box.createVerticalStrut(20); JButton btt1 = new JButton("BUTTON ONE"); // create a new dimension object Dimension d = new Dimension(200,40); // set the four kind of size, the button CANNOT be different than the dimension I choose btt1.setSize(d); btt1.setMinimumSize(d); btt1.setMaximumSize(d); btt1.setPreferredSize(d); JButton btt2 = new JButton("BUTTON TWO"); btt2.setSize(d); btt2.setMinimumSize(d); btt2.setMaximumSize(d); btt2.setPreferredSize(d); JButton btt3 = new JButton("BUTTON THREE"); btt3.setSize(d); btt3.setMinimumSize(d); btt3.setMaximumSize(d); btt3.setPreferredSize(d); // Now that the button are ready we put them in the panel. inside.add(btt1); inside.add(Box.createVerticalStrut(5)); inside.add(btt2); inside.add(box.createVerticalStrut(5)); inside.add(btt3); // Last white space, the bottom margin: inside.add(Box.createVerticalStrut(20));
Now I have a good structure. Just make it visible and all that he has done. Of course, you need to put it in a JFrame or JDialog, and, of course, the first panel can be JFrame or JDialog, because BoxLayout can be installed for any Component.
In this basic tutorial, I hope you understand the basics of programming such structures. But you will need to read this to continue and complicate the situation: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html http://docs.oracle.com/javase/tutorial/uiswing/ layout / box.html
have a nice day.