21-11-2012, 01:49 PM
Java
myGui.ppt (Size: 68 KB / Downloads: 99)
Swing vs. AWT
Swing is bigger and slower
Swing is more flexible and better looking
Swing and AWT are incompatible--you can use either, but you can’t mix them
Learning the AWT is a good start on learning Swing
AWT: Button b = new Button (“OK”);Swing: Jbutton b = new Jbutton(“OK”);
To build a GUI...
Make somewhere to display things--a Frame, a Window, or an Applet
Create controls (buttons, text areas, etc.)
Add your controls to your display area
Arrange, or lay out, your controls
Attach Listeners actions to your controls
Containers and Components
The job of a Container is to hold and display Components. A Container is also a Component
Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea
Some Container subclasses are Panel (and Applet), Window, and Frame
To create an applet
class MyApplet extends Applet { … }
this is the only way to make an Applet
You can add components to the applet
It’s best to add components in init( )
You can paint directly on the applet, but…
…it’s better to paint on another component
Do all painting from paint(Graphics g)
Arranging components
Every Container has a layout manager
The default layout for a Panel is FlowLayout
An Applet is a Panel
The default layout for a Applet is FlowLayout
You could set it explicitly with setLayout (new FlowLayout( ));
You could change it to some other layout manager
Making components active
Most components already appear to do something--buttons click, text appears
To associate an action with a component, attach a listener to it
Components send events, listeners listen for events
Different components may send different events, and require different listeners