DOC PREVIEW
UMBC CMSC 331 - GUI building with the AWT

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JavaAWT (Abstract Window Toolkit)SwingSwing vs. AWTTo build a GUI...Containers and ComponentsAn Applet is panel is a containerTo create an appletSome types of componentsCreating componentsAdding components to the AppletArranging componentsFlowLayoutBorderLayoutBorderLayout with five ButtonsUsing a PanelMaking components activeListenersWriting a ListenerMyButtonListenerListeners for TextFieldsSummary I: Building a GUISummary II: Building a GUIJavaGUI building with the AWTAWT (Abstract Window Toolkit)•Present in all Java implementations•Described in (almost) every Java textbook•Adequate for many applications•Uses the controls defined by your OS–therefore it's "least common denominator"•Difficult to build an attractive GUI•import java.awt.*;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 PCimport com.sun.java.swing.*; // UNIXSwing 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 controlsContainers 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 FrameAn Applet is panel is a containerjava.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet…so you can display things in itTo 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)Some types of componentsCreating 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);Adding components to the Appletclass MyApplet extends Applet { public void init () { add (lab); // same as this.add(lab) add (but); add (toggle); add (txt); add (scrolly);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 managerFlowLayout•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 uglyBorderLayout•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”));BorderLayout with five Buttonspublic 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"));}Using a Panel•panel p = new Panel();•add (BorderLayout.SOUTH, p);•p.add (new Button (“Button 1”));•p.add (new Button (“Button 2”));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 listenersListeners•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 methodsWriting 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) {…}MyButtonListenerpublic void init () { ... b1.addActionListener (new MyButtonListener ());}class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); }}Listeners for TextFields•An ActionListener listens for hitting the return key•An ActionListener demandspublic void actionPerformed (ActionEvent e)•use getText( ) to get the text•A TextListener listens for any and all keys•A TextListener demandspublic void textValueChanged(TextEvent e)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 likeSummary 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


View Full Document

UMBC CMSC 331 - GUI building with the AWT

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download GUI building with the AWT
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view GUI building with the AWT and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view GUI building with the AWT 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?