JavaFAQ Home » Java Notes by Fred Swartz
A GUI (Graphical User Interface) is easy to create after you have
a few basic elements from these groups:
- Top-level Containers (eg, JFrame)
- Intermediate Containers (eg, JPanel, JMenuBar)
- Components (eg, JLabel, JButton, JTextField)
- Listeners (eg, ActionListener)
- Layouts (eg, FlowLayout, BorderLayout, GridBagLayout)
Top-level Containers are windows (JFrame, JDialog).
Each window has two intermediate containers - a menubar which contains
the menus, and the content pane which holds the components.
Intermediate Containers (eg, JPanel) contain components.
Components are added to the intermediate container.
Every container has a layout manager, which specifies how the
components are arranged in the container.
Layouts specify how to arrange components in a JPanel (or other intermediate container).
Every JPanel starts with a default layout manager, but it's better
to set it explicitly..
Use one of Java's layout managers: FlowLayout,
BorderLayout, GridLayout, GridBagLayout, BoxLayout, etc.
Components are the user interface controls like buttons (JButton, ...),
text boxes (JTextField, JTextArea, ...), menus, etc.
These are added to a container with the add()
method, and the place they appear on a container is determined by
the layout which is used for that container.
Listeners are attached to components and contain the code that is called when
the component is used (eg, a button is clicked).
This is how a user action on a component (like a click on
a JButton or moving a JSlider)
is connected to a Java method.
As long as you work with components, you never have to use low-level listeners
(eg, mouse listeners and key listener) because the components take care of this
The above are essential elements for user
interfaces, but others will be useful in some programs. Eg, drawing graphics,
animation, mouse, keyboard, sound, ...
Good guidance on the appearance can be found in Sun's
Java Look and Feel Design Guidelines, which is available on-line
for free (java.sun.com/products/jlf/ed2/book/).
There are some simple patterns using constructors,
anonymous listeners, etc to produce a reasonable program.
Tools to help build a GUI
Some IDEs have GUI editors.
There are also separate GUI editors, eg
Printer Friendly Page
Send to a Friend
Search here again if you need more info!