So I am a computer science student and I’ve finished my first year. I wanted to create a simple program and I realized that I am so tired of using no layout;
this.setLayout(null);
It is so tiresome to add bounds to every single component. Well, I have been using JPanel
components and GridLayout
a lot, which have made my work a bit easier. But I am tired of it.
I care very much about the look of the GUI I make and use almost half the time programming to make the GUI look good before I start adding the functionality of the code. By not using a layout and adding bounds I am forced to setResizable(false)
because it looks bad if I change the size of the JFrame.
I’ve been searching a bit, and I know of BorderLayout
, and FlowLayout
, but I don’t like them. Is there any Layout that keeps the relative size of the components with respect to the size of the window?
For example I want to make a simple program that looks like this: (Quick sketch in Photoshop)
I can easily make this with 3 panels, but as I said, if I change the size of the frame everything stays in place instead of being relative to the window-size.
Can you guys help me?
5
Answers
This design looks for me to fit the BorderLayout, where in the NORTH you have the
values that changes
the CENTER you have the main part, and the SOUTH you have the buttons.Link to the Oracle Border Layout
You can apply this BorderLayout to the JFrame, then create 3 JPanels for each of the NORTH,CENTER and SOUTH sections. If you want to use responsive design for the components and panels, take a look at GridBagLayout which is much more flexible than the GridLayout
Try GridBagLayout.
Your sketch is actually quite close to the one of the examples in the official tutorial.
Layout management is a very complex problem, I don’t think people really appreciate just how complex it really is.
No one layout is ever going to achieve everything your want, in most cases, you will need to resort to two or more layouts, especially as your requirements become more complex.
For example, the following is simply a
BorderLayout
at the base and the buttons on aJPanel
using aFlowLayout
Which is achieved by using
For more complex layouts, I would consider using something like
GridBagLayout
. You may also want to considerMigLayout
as an alternativeTake a look at Laying Out Components Within a Container for more details about using layout managers
I’d like to use the combination of BorderLayout and BoxLayout. BorderLayout let me put the component based on their relative location’s relation and BoxLayout let me manage the subtle distance ( create some white space). You can use component.setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right)); to achieve this goal too.
Here is a demo and hope it can help you.
Here is the effect:
You can use BoxLayout for ButtonPanel if you don’t want to let the button’s size change.
And the effect is like this:
For more infomation about using BoxLayout to generate whitespace, you can refer to https://stackoverflow.com/a/22525005/3378204
HVLayout keeps the relative size of the components with respect to the size of the window, that is, if you configure components to have a relative size (e.g. buttons usually do not grow or shrink – they stick to their preferred size). This SO question was one of the motivations for me to push HVLayout to a release and a screenshot is included (showing big window size, smalll size and preferred “default” size):
Source code for the window is in RelativeToWindowSize.java
A number of helper-classes from HVLayout are used to build the window, so I don’t think it will be of much use here, but to get an impression, the “build window” part shown below: