Containers hold and organize your Components, but they also contain code for event
handling and many 'niceties' such as controlling the cursor image and the application's
icon.
Containers:
Components are generally the stuff that the user interacts with. The meat and potatoes of your application. (as discussed in the Introduction).
List of common Components
Lets walk through Sun's example.
public class ComponentTest extends Applet { Panel center;
The following code does
public void init() { setBackground(Color.lightGray); setFont(new Font("Helvetica", Font.PLAIN, 12)); setLayout(new BorderLayout()); Label l = new Label("North"); l.setAlignment(Label.CENTER);
add("South", new TextField("South")); add("North", l); center = new ScrollPanel(); center.setLayout(new BorderLayout()); add("Center", center);
Panel innerPanel = new ScrollPanel(); center.add("Center", innerPanel); innerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); innerPanel.add(new Label("List of Widgets")); innerPanel.add(new Button("Arthur")); innerPanel.add(new Button("Sami")); innerPanel.add(new SillyWidget()); innerPanel.add(new TextField("quite random")); innerPanel.add(new Scrollbar(Scrollbar.VERTICAL)); innerPanel.add(new Scrollbar(Scrollbar.HORIZONTAL));
List li = new List(4, false); li.reshape(0, 0, 100, 100); li.addItem("Arthur"); li.addItem("Sami"); li.addItem("James"); li.addItem("Chris"); li.addItem("Mary"); li.addItem("Lisa"); li.addItem("Kathy"); li.select(1);
innerPanel.add(li); TextArea tx = new TextArea(10, 5); tx.setText("this is some random text"); tx.setForeground(Color.red); innerPanel.add(tx);
Choice opt = new Choice(); opt.addItem("Arthur"); opt.addItem("Sami"); opt.addItem("Chris"); opt.addItem("Jim"); innerPanel.add(opt);
CheckboxGroup g = new CheckboxGroup(); innerPanel.add(new Checkbox("one", g, false)); innerPanel.add(new Checkbox("two", g, true)); innerPanel.add(new Checkbox("three", g, false)); center.add("East", new Scrollbar(Scrollbar.VERTICAL)); center.add("South", new Scrollbar(Scrollbar.HORIZONTAL)); add("East", new Button("East")); add("West", new Button("West")); }
public synchronized boolean handleEvent(Event e) { return false; } }
Definition of the ScrollPanel used above
class ScrollPanel extends Panel { public ScrollPanel() { reshape(0, 0, 100, 100); } // Overriden paint method that draws a rectange around the panel public void paint(Graphics g) { Rectangle r = bounds(); g.drawRect(0, 0, r.width, r.height); } }
The SillyWidget should look like this:
class SillyWidget extends Canvas { Color c1 = Color.pink; Color c2 = Color.lightGray; long entered = 0; long inComponent = 0; Font font = new Font("Courier", Font.BOLD, 12); FontMetrics fm; public SillyWidget() { reshape(0, 0, 100, 100); } // Called when the mouse enters the Canvas // This widget is just change the colors and forcing a repaint when the // mouse enters public boolean mouseEnter(Event e, int x, int y) { Color c = c1; entered = e.when; c1 = c2; c2 = c; repaint(); return true; } // Called when the mouse exits the Canvas // This widget is just change the colors and forcing a repaint when the // mouse exits public boolean mouseExit(Event e, int x, int y) { Color c = c1; inComponent = e.when - entered; c1 = c2; c2 = c; repaint(); return true; } // Called when the Canvas needs repaint and merely draws a bunch of rectangles public void paint(Graphics g) { Rectangle r = bounds(); g.setColor(c1); g.fillRoundRect(0, 0, r.width, r.height, 20, 20); g.setColor(c2); g.fillRoundRect(5, 5, r.width - 10, r.height - 10, 20, 20); g.setColor(c1); g.fillRoundRect(15, 15, r.width - 30, r.height - 30, 20, 20); g.setColor(Color.darkGray); g.setFont(font); fm = g.getFontMetrics(font); String s = ""+inComponent+" ms."; int width = fm.stringWidth(s); System.out.println("width = " + width); g.drawString(s, (r.width - width) / 2, (r.height - fm.getHeight()) / 2); } }
Q: Where can I find documentation and usage of Containers/Components?
A: You can find the here. You can also look through the example programs in the src/java/awt/test directory.Q: Can I find a quick and dirty list of constructors for them?
A: Here is a quick list:
Components:
- Button()
- Button(String label)
- Canvas()
- Checkbox()
- Checkbox(String label)
- Checkbox(String label, CheckboxGroup group, boolean state)
- Choice()
- Label()
- Label(String label )
- Label(String label, int alignment)
- List()
- List(int numOfLines, boolean allowMultipleSelect)
- Scrollbar()
- Scrollbar(int orientation)
- Scrollbar(int orientation, int value, int visible, int minimum,int maximum)
- TextArea()
- TextArea(int rows, int cols)
- TextArea(String text)
- TextArea(String text, int rows, int cols)
- TextField()
- TextField(int cols)
- TextField(String text)
- TextField(String text, int cols)
Containers:
- Dialog(Frame parent, boolean isModal)
- Dialog(Frame parent, String title, boolean isModal)
- Panel()
- Frame()
- Frame(String title)
- Window(Frame parent)