Everytime you want a window to appear on screen, use a JFrame.
If you want to make a custom GUI component (with shapes) then extend JPanel.
paintComponent(Graphics canvas)
method. The Graphics objects has methods to draw arcs, rectangles,
ellipses, etc.
In order to get input from the mouse your GUI components must implement Listeners. Each listener reacts to user input in some way.
mouseClicked
) and fill in those
method bodies with code that
reacts to the event.
addMouseListener
for JPanels
).
Swing provides a bunch of interactive widgets you can use for
building GUIs: JButton
s,
JTextField
s, JLabel
s,
JSlider
s, ...
ActionListener
)
Layout managers are used to lay out different GUI components in a JPanel. You can nest layout managers inside each other
Relevant managers:
GridLayout
: Arranges components row-wise in a grid
FlowLayout
: The default.
Arranges components left to right with
"word wrap". The layout changes as the user grows and shrinks the
window.
BorderLayout
: Made of five different layouts:
NORTH, SOUTH,
EAST, WEST, CENTER. You can put JPanel
s
inside each of these. This
is good for applications which should be surrounded by buttons.
CalculatorButton
which updated the display and
had an abstract
pressed
method. Subclasses of
CalculatorButton
implemented
operators, digits, and so on.