CS 4773 Object Oriented Systems Review of Swing Graphics Programming
- The three main types of objects used by Swing are:
JFrame, JPanel and JComponent
- The JFrame is the main top level container (along with JDialog and JApplet).
- Typically you add JPanels to a JFrame and add JComponents to the JPanels.
- JPanels are actually added to the ContentPane of the JFrame, not to the
JFrame itself.
Java 5 lets you add them to the JFrame, but you should not do this.
- The ContentPane has default layout BorderLayout.
- If you add a JPanel to the ContentPane, by default it is added to the
CENTER of the ContentPane and tries to fill the entire ContentPane.
- Two ways to set the size of a JFrame:
setSize(int w, int h);
pack();
- Use pack if you are setting the size of the individual JPanels or their
JComponents and want the JFrame to be as small as possible.
Note that it is sometimes tricky to do this.
- The default layout of JPanels is FlowLayout.
-
Here is some information on the different layouts available.
- Draw in a JPanel by overriding the method: paintComponent(Graphics g)
- This should start by calling its parent's paintComponent.
- Force paintComponent to execute by calling the JPanel's repaint() method.