JavaAWT (Abstract Window Toolkit)SwingSwing vs. AWTTo build a GUI...Containers and ComponentsAn Applet is Panel is a ContainerExample: A "Life" appletAppletsTo create an appletSome types of componentsCreating componentsAdding components to the AppletCreating a FrameArranging componentsFlowLayoutComplete example: FlowLayoutBorderLayoutBorderLayout with five ButtonsComplete example: BorderLayoutUsing a PanelGridLayoutComplete example: GridLayoutMaking components activeListenersWriting a ListenerMyButtonListenerListeners for TextFieldsAWT and SwingSummary I: Building a GUISummary II: Building a GUIVocabularyThe EndJavaGUI building with the AWT2AWT (Abstract Window Toolkit)Present in all Java implementationsDescribed in most Java textbooksAdequate for many applicationsUses the controls defined by your OStherefore it's “least common denominator”Difficult to build an attractive GUIimport java.awt.*;import java.awt.event.*;3SwingSame concepts as AWTDoesn’t work in ancient Java implementations (Java 1.1 and earlier)Many more controls, and they are more flexibleSome controls, but not all, are a lot more complicatedGives a choice of “look and feel” packagesMuch easier to build an attractive GUIimport javax.swing.*;4Swing vs. AWTSwing is bigger, slower, and more complicatedBut not as slow as it used to beSwing is more flexible and better lookingSwing and AWT are incompatible--you can use either, but you can’t mix themActually, you can, but it’s tricky and not worth doingLearning the AWT is a good start on learning SwingMany of the most common controls are just renamedAWT: Button b = new Button ("OK");Swing: JButton b = new JButton("OK");5To build a GUI...Make somewhere to display things—usually a Frame or Dialog (for an application), or an AppletCreate some Components, such as buttons, text areas, panels, etc.Add your Components to your display areaArrange, or lay out, your Components Attach Listeners to your Components Interacting with a Component causes an Event to occurA Listener gets a message when an interesting event occurs, and executes some code to deal with it6Containers and ComponentsThe job of a Container is to hold and display ComponentsSome common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextAreaA Container is also a ComponentThis allows Containers to be nestedSome Container subclasses are Panel (and Applet), Window, and Frame7An 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 an Applet8Example: A "Life" appletContainer (Applet)Containers (Panels)Component (Canvas)Components (Buttons)Components (Labels)Components (TextFields)9AppletsAn application has a public static void main(String args[ ]) method, but an Applet usually does notAn Applet's main method is in the BrowserTo write an Applet, you extend Applet and override some of its methodsThe most important methods are init( ), start( ), and paint(Graphics g)10To create an appletpublic class MyApplet extends Applet { … }this is the only way to make an AppletYou can add components to the appletThe best place to add components is in init( )You can paint directly on the applet, but……it’s better to paint on a contained componentDo all painting from paint(Graphics g)11Some types of componentsLabelButtonButtonCheckboxChoiceListScrollbarTextFieldTextAreaCheckboxGroupCheckbox12Creating 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);13Adding 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); ...14Creating a FrameWhen you create an Applet, you get a Panel “for free”When you write a GUI for an application, you need to create and use a Frame:Frame frame = new Frame();frame.setTitle("My Frame");frame.setSize(300, 200); // width, height... add components ...frame.setVisible(true);Or:class MyClass extends Frame { ... setTitle("My Frame"); // in some instance method15Arranging componentsEvery Container has a layout managerThe default layout for a Panel is FlowLayoutAn Applet is a PanelTherefore, the default layout for a Applet is FlowLayoutYou could set it explicitly with setLayout (new FlowLayout( ));You could change it to some other layout manager16FlowLayoutUse add(component); to add to a component when using a FlowLayoutComponents are added left-to-rightIf no room, a new row is startedExact layout depends on size of AppletComponents are made as small as possibleFlowLayout is convenient but often ugly17Complete example: FlowLayoutimport java.awt.*;import java.applet.*;public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); // default add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); }}18BorderLayoutAt most five components can be addedIf you want more components, add a Panel, then add components to it.setLayout (new BorderLayout());add (new Button("NORTH"), BorderLayout.NORTH);19BorderLayout with five Buttonspublic void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER);}20Complete example: BorderLayoutimport java.awt.*;import java.applet.*;public class BorderLayoutExample extends Applet { public void init () { setLayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"),
View Full Document