Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.

Similar presentations


Presentation on theme: "Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12."— Presentation transcript:

1 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12 The Abstract Window Toolkit

2 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 2 12.1 Overview AWT (Abstract Window Toolkit) –Classes and tools for building GUI Swing (Not using here) –Newer library for GUI –More powerful and sophisticated than the AWT Creating a Graphical User Interface –Components : an object that the user can see on the screen and interact with (Ex. button, scrollbar) –Container : a component that’s capable of holding other components (Ex. Windows, frame) –Events : an action triggered by the user (Ex. a key press, mouse click)

3 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 3 12.2 Frames Frame is a window with a title and a border or menu. (DrawableFrame is a subclass of Frame) Frame Methods –Point getLocation() –Demension getSize() –void pack() –void setLocation(int x, int y) –void setSize(int width, int height) –void setVisible(boolean b)

4 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 4 Creating a Frame A simple program that creates a Frame object and displays it on the screen import java.awt.*; public class FrameTest { public static void main(String[] args) { Frame f = new Frame(“Frame Test”); f.setSize(150, 100); f.setVisible(true); }

5 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 5 Frame methods // Check the methods of frame in FrameTest2.javaFrameTest2.java

6 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 6 Adding Components to a sub class of a Frame ButtonTestFrame : A subclass of Frame import java.awt.*; import java.awt.event.*; public class ButtonTest { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button(“Testing”); add(b); }

7 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 7 Adding Components to a Frame ButtonTestAgain : Not use a subclass import java.awt.*; public class ButtonTestAgain { public static void main(String[] args) { Frame f = new Frame("Button Test"); f.setLayout(new FlowLayout()); Button b = new Button("Testing"); f.add(b); f.setSize(150, 100); f.setVisible(true); f.pack(); }

8 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 8 12.3 Event Listeners Event –ActionEvent A Sigificant action has been performed on a component E.x Pressing button, pressing enter key in text field.. –AdjustmentEvent The state of an adjustable component(such as a scrollbar) has changed –ItemEvent An item has been selected (or deselected) within a checkbox, choice menu, or list –TextEvent The contents of a text area or text field have changed –WindowEvent: low-level event. It happens when the user attempts to close a window

9 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 9 Interface Interface looks like a class, but its methods are not fully defined. –Name, Parameter list,Result type –NO body, though Example : ActionListener public interface ActionListener extends EventListener{ public void actionPerformed(ActionEvent evt); } Interface is a pattern. A class implements an interface, that is, write the body of the method of interface.

10 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 10 Event Listeners … Listener Interfaces (ButtonTest2.java)) –ActionListener : actionPerformed(ActionEvent evt) –AdjustmentListener :AdjestmentValueChanged(AdjustmentEvent evt) –ItemListent : itemStatChanged(ItemEvent evt) –TexListener : TextValueChanged(TextEvent evt) public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent ext); } class class-name implements ActionListener { public void actionPerformed(ActionEvent ext) { …. // body } … //Variables, constructors, and methods, if desired }

11 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 11 Event Listeners … Creating Event Listeners class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ext) { …. } Botton objectBottonListener object * Registering single listener object to a single components Button b = new Button(“Change Color”); ButtonListener listener = new ButtonListener(); b.addActionListener(listener);

12 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 12 Event Listeners … ButtonTest2.java // Displays a frame containing a single "Close window" // button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest2 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); }

13 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 13 Continued ButtonTest2.java …. // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window"); add(b); b.addActionListener(new ButtonListener()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); }

14 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 14 Event Listeners … Work with the. Implements WindowListener interface is a bad idea, as we do not need all of the seven methods. WindowAdapter (Adapter) Class –Implement WindowListener interface –Include empty methods so we can extend those methods * Installing WindowListener to frame … addWIndowListenr ( new WindowCloser () );.. // Listener for window class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); }

15 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 15 Add Adapter class ButtonTest3.java // Displays a frame containing a single "Close window" // button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest3 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window");

16 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 16 Continued ButtonTest3.java add(b); b.addActionListener(new ButtonListener()); // Attach window listener addWindowListener(new WindowCloser()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); } // Listener for window class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); }

17 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 17 12.4 Inner Classes Inner Classes ( ChangeColor.java ) –A class that’s nested inside another class –The methods in an inner class have access to the variables and methods of the enclosing class, allowing the inner class to serve as a “helper” for the enclosing class –(Move the ButtonListener class inside of a frame class.) class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { if (getBackground() == Color.white) setBackground(Color.black); else setBackground(Color.white); } * Code to change background color of a frame.

18 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 18 12.5 Attaching Listeners to Multiple Components Single listener to multiple components The methods to determine which component is involved –getSource() : return an object reference (ChangeColor4.java) Ex. Object source = evt.getSource(); If (source == testButton) … –getActionCommand() : return the name (label) associated with an action event Ex. String label = evt.getActionCommand(); If (label.equals(“Testing”)) … (ChangeColor2.java) Botton object BottonListener object Botton object

19 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 19 Separate Listeners Multiple listeners to multiple components (ChangeColor3.java) –Implement separate listeners for each componenets Botton objectBottonListener2 object Botton objectBottonListener1 object // Listener for "Lighter" button class LighterButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { setBackground(getBackground().brighter()); } // Listener for "Darker" button class DarkerButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { setBackground(getBackground().darker()); }

20 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 20 12.6 Layout Layout manager –determine the sizes and positions of components within the container Class NameBehavior BorderLayoutArranges components along the sides of the container and in the middle CardLayoutArrange components in “cards.” Only one card is visible at a time FlowLayoutArranges components in variable-length rows GridBagLayoutAligns components horizontally and vertically components can be of different sizes GridLayoutArranges components in fixed-length rows and columns

21 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 21 Layout The FlowLayout Class Ex1. setLayout(new FlowLayout() ); Ex2. setLayout(new FlowLayout(FlowLayout.LEFT)); Ex3. setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10) );//horizontal and vertical gap The GridLayout Class Ex1. setLayout(new GridLayout(4, 5) ); Ex2. setLayout(new GridLayout(4, 5, 20, 10) );

22 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 22 Layout The BorderLayout Class Ex1. setLayout(new BorderLayout() ); Ex2. setLayout(new BorderLayout(20, 10) ); cf) add(“Center”, new Button(“Test”));

23 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 23 Preferred Sizes How layout managers work? According to “preferred size” of each component. –FlowLayout : Honors the preferred sizes of all components. Buttons size is not changing. –GridLayout : Ignores the preferred sizes of all components. –BorderLayout : Honors the preferred widths of the East and West components. Honors the preferred heights of the North and South components. Ignores the preferred size of the Center component.

24 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 24 Panels A panel—an instance of the Panel class—is another kind of container. A panel is rectangular but has no border. When a panel is placed inside another container, it blends in seamlessly. Each panel has its own layout manager. A panel can be used to create a group of components that is treated as a single component.

25 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 25 Panels A figure showing the panels as dashed rectangles:

26 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 26 Panels Statements to create the phone layout: (SimPhone.java) Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(4, 3, 10, 10)); for (int i = 1; i <= 9; i++) buttonPanel.add(new Button(i + "")); buttonPanel.add(new Button("*")); buttonPanel.add(new Button("0")); buttonPanel.add(new Button("#")); Panel centerPanel = new Panel(); centerPanel.add(buttonPanel); add("Center", centerPanel); Panel bottomPanel = new Panel(); bottomPanel.add(new Button("Dial")); add("South", bottomPanel);

27 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 27 12.7 Creating and Using Components How to create the components? What kind of event(s) it fires? How to determine the current state of the component?

28 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 28 Checkboxes (CheckBoxDemo.java) Ex1. Checkbox cb = new Checkbox(“Enable sounds”); // No label Ex2. Checkbox cb = new Checkbox( ); // “on” Ex3. Checkbox cb = new Checkbox(“Enable sounds”, true); To detect this event, we need to implements the ItemListener interface, which requires “itemStateChanged()” method class CheckboxListener implements ItemListener { public void itemStateChanged(ItemEvent evt) { … } boolean getState(), void setState(boolean st)

29 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 29 Checkbox Groups (CheckBoxGroupDemo.java) Ex1. CheckboxGroup musicGroup = new CheckboxGroup(); Checkbox rockBox = new Checkbox(“Rock”, musicGroup, true); Checkbox jazzBox = new Checkbox(“Jazz”, musicGroup, false); Checkbox classicalBox = new Checkbox(“Classical”, musicGroup, false);

30 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 30 Choice Menus Ex1. Choice countryChoice = new Choice(); countryChoice.add(“U.S.A”); countryChoice.add(“Canada”); countryChoice.add(“Mexico”); To detect this event, we need to implements the ItemListener interface, which requires “itemStateChanged()” method class ChoiceMenuListener implements ItemListener { public void itemStateChanged(ItemEvent evt) { … } String getSelectedItem(), int getSelectedIndex()

31 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 31 Labels Ex1. Label lastName = new Label(“Enter last name : ”); String getText(), void setText() No border. Often placed next to other components to indicate their meaning or function. The user can’t change a label’s text; there are no events defined for labels. Ex) PickColor PickColor

32 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 32 Lists A list is a rectangle containing a series of items: If not all list items are visible, a scrollbar appears to the right of the list:

33 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 33 Lists Ex1. List countryList = new List(); Ex2. List countryList = new List(5); countryList.add(“U.S.A”); countryList.add(“Canada”); countryList.add(“Mexico”); String getSelectedItem(), int getSelectedIndex() Two events: –Single-clicking on a list item causes an item event. –Double-clicking causes an action event. Ex: ShowDefinitionShowDefinition

34 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 34 Scrollbars A scrollbar is a sliding bar. Scrollbars can be either horizontal or vertical: Create a scrollbar. (type, initial-value, width, min, max) Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL,50, 1, 0, 100);

35 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 35 Scrollbars To detect this event: class ScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent evt) { … } sb.addAdjustmentListener(new ScrollbarListener()); Get: int value = sb.getValue(); Set: sb.setValue(newValue); Ex:PickColorPickColor

36 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 36 Text Areas A text area is capable of displaying multiple lines of text: Scrollbars at the bottom and right side make it possible for the user to view text that’s not otherwise visible.

37 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 37 Text Areas How to create a TextArea object: –TextArea ta = new TextArea(); –String quote = “ ….”; TextArea ta = new TextArea(quote); –TextArea ta = new TextArea(10, 20); // rows, columns –TextArea ta = new TextArea(quote, 10, 20); Set editable or not editable, default is true: –ta.setEditable(false);//not editable Detect an event class TextAreaListener implements TextListener { public void textValueChanged(TextEvent evt) { … } ta.addTextListener(new TextAreaListener());

38 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 38 Text Areas Methods: String text = ta.getText(); ta.setText("Line 1\nLine 2\nLine 3"); ta.append("\nLine 4"); Ex) TextAreaDemo ShowDefinition

39 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 39 Text Fields A text field contains a single line of text: (ex: ConvertTemp) ConvertTemp How to create: TextField tf = new TextField(); TextField tf = new TextField("Your name here"); TextField tf = new TextField(40); TextField tf = new TextField("Your name here", 40); To detect this event, we need to implements the ActionListener. class textFieldListner implements ActionListener { public void actionPerformed(ActionEvent evt) { … }

40 Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 40 12.8Examples The ConvertTemp, ShowDefinition, and PickColor programs illustrate the use of various GUI components. ConvertTemp ShowDefinition PickColor


Download ppt "Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12."

Similar presentations


Ads by Google