Next: Layouts.
Up: Composing GUIs.
Previous: None.
The JPanel class
Textbook: Section 12.6
A JPanel object is an empty graphical component. It may seem weird
that such a thing is worth talking about. But it is, for two reasons:
- By extending the JPanel class, you can define your own custom
component. For example, Logisim (the logic circuit simulator used in
CSCI 150) extends JPanel to create the canvas on which the user can draw
a circuit. This is an advanced topic, which we're not going to discuss
today. (You can read about it on the class handouts Web page.)
- A JPanel is a subclass of Container, which means you can add other
components into it.
This last point is the reason we want to talk about panels today.
There are three JPanel methods that are particularly important.
- JPanel()
- Creates an empty JPanel.
- void add(Component what)
- Inserts what into the panel, using the default placement.
- void add(Component what, Object info)
- Inserts what into the panel, using info as
information about where the object should be placed.
Next: Layouts.
Up: Composing GUIs.
Previous: None.