Most windows are a Container that can hold other windows or GUI components. Canvas is the major exception.
Layout Managers
Containers have a LayoutManager that automatically sizes and positions components that are in the window. You can change the behavior of the layout manager or disable it completely.
Events
Windows and components can receive events.
Popup Windows
Some windows (Frame and Dialog) have their own title bar and border and can be placed at arbitrary locations on the screen
Other windows (Canvas an Panel) are embedded into existing windows only
Canvas Class
Major Purposes
A drawing area
A custom Component that does not need to contain any other Component (e.g. an image button)
Default Layout Manager - None
Canvas cannot contain any other Components
Creating and Using
Create the Canvas Canvas canvas = new Canvas();
Or, since you typically create a subclass of Canvas that has customized drawing via its paint method:
SpecializedCanvas canvas = new SpecializedCanvas();
Canvas (Continued)
Creating and Using, cont.
Size the Canvas
canvas.setSize(width, height);
Add the Canvas to the current Window
add(canvas);
or depending on the layout manager you can position the Canvas
add(canvas, BorderLayout.Region_Name);
If you first create a separate window (e.g. a Panel), then put the Canvas in the window using something like
someWindow.add(canvas);
Canvas Example
import java.awt.*;
/** A Circle component built using a Canvas. */
public class Circle extends Canvas {
private int width, height;
public Circle(Color foreground, int radius) {
setForeground(foreground);
width = 2*radius;
height = 2*radius;
setSize(width, height);
}
public void paint(Graphics g) {
g.fillOval(0, 0, width, height);
}
public void setCenter(int x, int y) {
setLocation(x - width/2, y - height/2);
}
}
Component Class
Direct Parent Class of Canvas
Ancestor of all Window Types
Useful Methods
getBackground/setBackground
getForeground/setForeground
Change/lookup the default foreground color
Color is inherited by the Graphics object of the component
getFont/setFont
Returns/sets the current font
Inherited by the Graphics object of the component
paint
Called whenever the user call repaint or when the component is obscured and reexposed
Component Class (Continued)
Useful Methods
setVisible
Exposes (true) or hides (false) the component
Especially useful for frames and dialogs
setSize/setBounds/setLocation
getSize/getBounds/getLocation
Physical aspects (size and position) of the component
list
Prints out info on this component and any components it contains; useful for debugging
invalidate/validate
Tell layout manager to redo the layout
getParent
Returns enclosing window (or null if there is none)
Panel Class
Major Purposes
To group/organize components
A custom component that requires embedded components
Default Layout Manager - FlowLayout
Shrinks components to their preferred size
Places them left to right in centered rows
Creating and Using
Create the Panel
Panel panel = new Panel();
Add Components to Panel
panel.add(someComponent);
panel.add(someOtherComponent); ...
Panel (Continued)
Creating and Using, continued
Add Panel to Container
To an external container
container.add(panel);
From within a container
add(panel);
Note the lack of an explicit setSize
The components inside determine the size of a panel; the panel is no larger then necessary to hold the components
A JApplet contains a content pane in which to add components. Changing other properties like the layout manager, background color, etc., also applies to the content pane. Access the content pane through getContentPane.
Layout manager
The default layout manager is BorderLayout (as with Frame and JFrame), not FlowLayout (as with Applet). BorderLayout is really layout manager of content pane.
Look and feel
The default look and feel is Java (Metal), so you have to explicitly switch the look and feel if you want the native look.
JApplet: Example Code
import java.awt.*;
import javax.swing.*;
public class JAppletExample extends JApplet {
public void init() {
WindowUtilities.setNativeLookAndFeel();
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
}
}
Swing Equivalents of AWT Components
JLabel
New features: HTML content images, borders
JButton
New features: icons, alignment, mnemonics
JPanel
New feature: borders
JSlider
New features: tick marks and labels
JButton
Main new feature: icons
Create an ImageIcon by passing the ImageIcon constructor a String representing a GIF or JPG file (animated GIFs!).
Pass the ImageIcon to the JButton constructor.
Other features
HTML content as with JLabel
Alignment: location of image with respect to text
Mnemonics: keyboard accelerators that let you use Alt-someChar to trigger the button.