GUI Swing and AWT
-
Upload
hemantscrib -
Category
Documents
-
view
235 -
download
0
Transcript of GUI Swing and AWT
-
8/3/2019 GUI Swing and AWT
1/24
Java
GUI building with the AWT
-
8/3/2019 GUI Swing and AWT
2/24
AWT (Abstract Window Toolkit)
Present in all Java implementations
Described in (almost) every Java textbook Adequate formany applications
Uses the controls defined byyourOS
therefore it's "least common denominator" Difficult to build an attractive GUI
import java.awt.*;
-
8/3/2019 GUI Swing and AWT
3/24
Swing
Requires Java 2 ora separate (huge)
download
More controls, and they are more flexible
Gives a choice of look and feel packages
Much easierto build an attractive GUI import javax.swing.*; // Mac or PCimport com.sun.java.swing.*; // UNIX
-
8/3/2019 GUI Swing and AWT
4/24
Swingvs. AWT
Swing is biggerand slower
Swing is more flexible and betterlooking Swing and AWT are incompatible--you can
use either, but you cant mix them
Learning the AWT is a good start on learningSwing
AWT: Button b = new Button (OK);
Swing: Jbutton b = new Jbutton(OK);
-
8/3/2019 GUI Swing and AWT
5/24
To builda GUI...
Make somewhere to display things--a
Frame, a Window, oran Applet
Create controls (buttons, text areas, etc.)
Add yourcontrols to yourdisplay area
Arrange, orlay out, yourcontrols Attach Listeners actions to yourcontrols
-
8/3/2019 GUI Swing and AWT
6/24
Containers andComponents
The job of a Container is to hold and displayComponents. A Container is also aComponent
Some common subclasses ofComponent areButton, Checkbox, Label, Scrollbar,
TextField, and TextArea Some Container subclasses are Panel (and
Applet), Window, and Frame
-
8/3/2019 GUI Swing and AWT
7/24
A Frame A Window in aContainer
java.lang.Object|
+----java.awt.Component|+----java.awt.Container
|
+----java.awt.Window|+----java.awt.Frame
|+----java.swing.JFrame
-
8/3/2019 GUI Swing and AWT
8/24
To Create A Frame
import java.awt.*; Frame newFrame = new Frame( );
class MyFrame extends Frame { }
We can add components to the Frame
Its best to add components in init( )
-
8/3/2019 GUI Swing and AWT
9/24
Some types ofcomponents
-
8/3/2019 GUI Swing and AWT
10/24
10
CreatingComponents
Swing
// Create a button with text OK
JButton jbtOK = new JButton("OK");
// Create a label with text "Enter your name: "
JLabel jlblName = new JLabel("Enter your name: ");
// Create a text field with text "Type Name Here"
JTextField jtfName = new JTextField("Type Name Here");
// Create a check box with text bold
JCheckBox jchkBold = new JCheckBox("Bold");
// Create a radio button with text red
JRadioButton jrbRed = new JRadioButton("Red");
// Create a combo box with choices red, green, and blue
JComboBox jcboColor = new JComboBox(new String[]{"Red",
"Green", "Blue"});
Button
Label
Text
field
Check
Box
Radio
Button
Combo
Box
-
8/3/2019 GUI Swing and AWT
11/24
Creatingcomponents
AWT Label lab = new Label (Hi, Dave!");
Button but = new Button ("Click me!");
Checkbox toggle = new Checkbox (toggle");
TextField txt =
new TextField (Initial text.", 20);
Scrollbar scrolly = new Scrollbar
(Scrollbar.HORIZONTAL, initialValue,bubbleSize, minValue, maxValue);
-
8/3/2019 GUI Swing and AWT
12/24
Addingcomponents to the Frame
class MyApplet extends Applet {
public void init () {
add (lab); // same as this.add(lab)
add (but);
add (toggle);
add (txt);
add (scrolly);
-
8/3/2019 GUI Swing and AWT
13/24
Arrangingcomponents
EveryContainer has a layout manager
The default layout fora Frame is FlowLayout
We can set it explicitly withsetLayout (new FlowLayout( ));
We can change it to some otherlayout manager
-
8/3/2019 GUI Swing and AWT
14/24
FlowLayout
Use add (component); to add to acomponent using a FlowLayout
Components are added left-to-right
If no room, a new row is started
Exact layout depends on size of Frame Components are made as small as possible
FlowLayout is convenient but often ugly
-
8/3/2019 GUI Swing and AWT
15/24
BorderLayout At most five components
can be added
Ifyou want more
components, add a Panel,
then add components to it.
setLayout (newBorderLayout());
add (BorderLayout.NORTH, new Button(NORTH));
-
8/3/2019 GUI Swing and AWT
16/24
BorderLayout with five Buttons
public void init() {
setLayout (new BorderLayout ());add (BorderLayout.NORTH, new Button ("NORTH"));add (BorderLayout.SOUTH, new Button ("SOUTH"));add (BorderLayout.EAST, new Button ("EAST"));add (BorderLayout.WEST, new Button ("WEST"));
add (BorderLayout.CENTER, new Button ("CENTER"));}
-
8/3/2019 GUI Swing and AWT
17/24
Usinga Panel
panel p = new Panel(); add (BorderLayout.SOUTH, p);
p.add (new Button (Button 1));
p.add (new Button (Button 2));
-
8/3/2019 GUI Swing and AWT
18/24
Makingcomponents active
Most components alreadyappearto do
something--buttons click, text appears
To associate an action with a component, attach
a listenerto it
Components send events, listeners listen for
events
Different components may send different
events, and require different listeners
-
8/3/2019 GUI Swing and AWT
19/24
Listeners
Listeners are interfaces, not classes
class MyButtonListener implementsActionListener {
An interface is a group of methods that must
be supplied
When you sayimplements, you arepromisingto supply those methods
-
8/3/2019 GUI Swing and AWT
20/24
Writinga Listener
Fora Button, you need an ActionListener
b1.addActionListener(new MyButtonListener ( ));
An ActionListener must have anactionPerformed( ) method
public voidactionPerformed(ActionEvent e) {}
-
8/3/2019 GUI Swing and AWT
21/24
MyButtonListenerpublic void init () {
...
b1.addActionListener (new MyButtonListener ());}
class MyButtonListener implements ActionListener {public void actionPerformed (ActionEvent e) {
showStatus ("Ouch!");}
}
-
8/3/2019 GUI Swing and AWT
22/24
Listeners for TextFields
An ActionListener listens forhitting thereturn key
An ActionListener demandspublic void actionPerformed (ActionEvent e)
use getText( ) to get the text
A TextListener listens forany and all keys
A TextListener demandspublic void textValueChanged(TextEvent e)
-
8/3/2019 GUI Swing and AWT
23/24
Summary I:
Buildinga GUI
Create A Frame by extending Frame
Choose a layout manager (FlowLayout isthe default)
Create more complex layouts by putting
Panels in the Frame; each Panel can haveits own layout manager
Create othercomponents and add them to
whichever Panels you like
-
8/3/2019 GUI Swing and AWT
24/24
Summary II:
Buildinga GUI
Foreach active component, look up what
kind ofListeners it can have
Create (implement) the Listeners, typicallyone Listenerperactive component
Foreach Listener you implement, supplythe methods that it requires