Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI building with the AWT

Similar presentations


Presentation on theme: "GUI building with the AWT"— Presentation transcript:

1 GUI building with the AWT
Java GUI building with the AWT

2 AWT (Abstract Window Toolkit)
Present in all Java implementations Described in (almost) every Java textbook Uses the controls defined by your OS therefore it's "least common denominator" Difficult to build an attractive GUI import java.awt.*;

3 Swing Requires Java 2 or a separate (huge) download
More controls, and they are more flexible Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*; // Mac or PC import com.sun.java.swing.*; // UNIX

4 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”);

5 GUI Component Menu Menu bar Combo box Text area Button Scroll bar List
Label

6 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

7 Buttons import java.awt.*; public class SimpleButton extends java.applet.Applet { Button helloButton = new Button(“Hello World!”); public void init() { add(helloButton); }}

8 Labels add(eMailLabel);
Label Label = new Label(" address: "); add( Label);

9 Text Fields add(txtName);
TextField txtName = new TextField(“Donny”, 10); add(txtName);

10 Text Areas TextArea comments = new TextArea(“This is on line 1\nThis is on line 2”, 3,20); add(comments);

11 Check Boxes Checkbox goodTutorial = new Checkbox(“Is this a good tutorial?”); add(goodTutorial);

12 CheckboxGroup age = new CheckboxGroup();
Checkbox chkUnder10 = new Checkbox(“Under 10 years old”, false, age); Checkbox chk10to15 = new Checkbox(“10 to 14 years old”, false, age); Checkbox chk15to20 = new Checkbox(“15 to 19 years old”, true, age); Checkbox chkOver20 = new Checkbox(“Over 20 years old”, false, age); add(chkUnder10); add(chk10to15); add(chk15to20); add(chkOver20);

13 Drop Down Lists Choice textAlignment = new Choice();
textAlignment.add(“Left”); textAlignment.add(“Center”); textAlignment.add(“Right”); textAlignment.add(“Random”); add(textAlignment);

14 GUI Class Hierarchy (Swing)

15 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

16 Container Classes Container classes can contain other GUI components.

17 GUI Helper Classes The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension.

18 Swing GUI Components

19 AWT (Optional)

20 An Applet is panel is a container
java.lang.Object | +----java.awt.Component +----java.awt.Container +----java.awt.Panel +----java.applet.Applet …so you can display things in it

21 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)

22 Some types of components

23 Creating components 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);

24 Adding components to the Applet
class MyApplet extends Applet { public void init () { add (lab); // same as this.add(lab) add (but); add (toggle); add (txt); add (scrolly);

25 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

26 FlowLayout Use add (component); to add to a component using a FlowLayout Components are added left-to-right If no room, a new row is started Exact layout depends on size of Applet Components are made as small as possible FlowLayout is convenient but often ugly

27 BorderLayout At most five components can be added
If you want more components, add a Panel, then add components to it. setLayout (new BorderLayout()); add (BorderLayout.NORTH, new Button(“NORTH”));

28 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")); }

29 Using a Panel panel p = new Panel(); add (BorderLayout.SOUTH, p);
p.add (new Button (“Button 1”)); p.add (new Button (“Button 2”));

30 Frame and JFrame import javax.swing.*; class MyFrame extends JFrame {
public MyFrame() { setTitle("My Empty Frame"); setSize(300,200); // default size is 0,0 setLocation(10,200); // default is 0,0 (top left corner) } public static void main(String[] args) { JFrame f = new MyFrame(); f.show(); } }

31 Panels Panel myPanel = new Panel(); myPanel.add(helloButton);
myPanel.add(goodbyeButton); add(myPanel);

32 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

33 How to Write an Action Listener
Action listeners are probably the easiest — and most common — event handlers to implement. You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

34 How to Write an Action Listener
Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example:public class MyClass implements ActionListener { Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }

35 Listeners Listeners are interfaces, not classes
class MyButtonListener implements ActionListener { An interface is a group of methods that must be supplied When you say implements, you are promising to supply those methods

36 Writing a Listener For a Button, you need an ActionListener
b1.addActionListener (new MyButtonListener ( )); An ActionListener must have an actionPerformed( ) method public void actionPerformed(ActionEvent e) {…}

37 MyButtonListener public void init () { ...
b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); }

38 Listeners for TextFields
An ActionListener listens for hitting the return key An ActionListener demands public void actionPerformed (ActionEvent e) use getText( ) to get the text A TextListener listens for any and all keys A TextListener demands public void textValueChanged(TextEvent e)

39 Summary I: Building a GUI
Create an applet by extending Applet Choose a layout manager (FlowLayout is the default) Create more complex layouts by putting Panels in the Applet; each Panel can have its own layout manager Create other components and add them to whichever Panels you like

40 Summary II: Building a GUI
For each active component, look up what kind of Listeners it can have Create (implement) the Listeners, typically one Listener per active component For each Listener you implement, supply the methods that it requires Finally, don't forget to supply the HTML


Download ppt "GUI building with the AWT"

Similar presentations


Ads by Google