Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.

Similar presentations


Presentation on theme: "Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces."— Presentation transcript:

1 Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces

2 Chapter 10 - Writing Graphical User Interfaces2 Understanding Java’s GUI Classes  Java has two sets of GUI classes  Original GUI class – AWT – Abstract Window Toolkit  Located in java.awt package  Updated GUI class – Swing  Located in javax.swing  Both contain classes to create controls  Windows  Push buttons  Text fields  Menus 10

3 Chapter 10 - Writing Graphical User Interfaces3 Understanding Java’s GUI Classes 10

4 Chapter 10 - Writing Graphical User Interfaces4 Understanding Java’s GUI Classes

5 Chapter 10 - Writing Graphical User Interfaces5 Understanding Java’s GUI Classes  Style and appearance of GUI components are called their look and feel  AWT classes adopt look and feel of the local platform  With Swing you have the option of using local platform look and feel or a standard look and feel (called “metal”)  Both AWT and Swing take advantage of inheritance 10

6 Chapter 10 - Writing Graphical User Interfaces6 Understanding Java’s GUI Classes 10

7 Chapter 10 - Writing Graphical User Interfaces7 Using AWT Classes  Class definition of a GUI generally follows the structure you used for a problem domain class  One difference is that you create an instance of GUI and make it visible  Write statements to instantiate GUI components  Then write statements to add them to the window

8 Chapter 10 - Writing Graphical User Interfaces8 Creating a Window with a Button  Frame inheritance hierarchy is Windows, Container, and Component  Your GUI inherits from all three super classes  Need to import the AWT package: import java.awt.*;  Class is a subclass of Frame – must extend the Frame class public class AWTFrameWithButton extends Frame

9 Chapter 10 - Writing Graphical User Interfaces9 Creating a Window with a Button  Constructor must accomplish five tasks: 1.Create and instance of Button 2.Add the button to the frame 3.Establish the frame size 4.Place a title on the frame 5.Make the frame visible 10

10 Chapter 10 - Writing Graphical User Interfaces10 Creating a Window with a Button

11 Chapter 10 - Writing Graphical User Interfaces11 Using Layout Managers  AWT includes several classes called layout managers  Used to determine how components are positioned on containers such as frames and panels  Most frequently used layout managers:  FlowLayout  BorderLayout  GridLayout

12 Chapter 10 - Writing Graphical User Interfaces12 Using Layout Managers  To use:  Instantiate the manager you want to use  Invoke the setLayout method, passing a reference to the layout manager you want to use  Components placed on a container using the BorderLayout manager expand to fill the space available 10

13 Chapter 10 - Writing Graphical User Interfaces13 Using Layout Managers

14 Chapter 10 - Writing Graphical User Interfaces14 Using Layout Managers  FlowLayout manager places components on the frame as you add them, left to right  GridLayout manager arranges the container into a grid consisting of rows and columns  When you instantiate you need to specify the number of rows and columns you want to have

15 Chapter 10 - Writing Graphical User Interfaces15 Using Layout Managers

16 Chapter 10 - Writing Graphical User Interfaces16 Handling Java Events  Users interact with GUI by entering data and clicking on components  An event is a signal that the user has taken some action  Event is also an instance of a class – such as MouseEvent, WindowEvent, etc.

17 Chapter 10 - Writing Graphical User Interfaces17 Handling Java Events 10

18 Chapter 10 - Writing Graphical User Interfaces18 Handling Java Events  Event listener registers with event source by invoking a method in the event source  Button events are called action events – registration method is named addActionListener  Next step is to write a method to handle the event – for buttons this method is actionPerformed

19 Chapter 10 - Writing Graphical User Interfaces19 Handling Java Events  There are three methods for handling events  Implementing interfaces  Extending Adapter classes  Creating Inner classes

20 Chapter 10 - Writing Graphical User Interfaces20 Implementing Interfaces  When you implement an interface you must override all of its methods  Implement the appropriate interface for the event  Write methods to override the interface’s methods and deal with the events  Interface for buttons is ActionListener  Need to import interface: import java.awt.event.*;

21 Chapter 10 - Writing Graphical User Interfaces21 Implementing Interfaces

22 Chapter 10 - Writing Graphical User Interfaces22 Extending Adapter Class  Second way of dealing with events is to use an adapter class  Supplied class that implements a listener interface, and then overrides all of the interface methods with null methods  Most override methods you want to respond to  Need to create two classes – one that extends Frame, and another that extends WindowAdapter

23 Chapter 10 - Writing Graphical User Interfaces23 Extending Adapter Class

24 Chapter 10 - Writing Graphical User Interfaces24 Creating Inner Classes  Third way of dealing with events is to use an anonymous inner class  Anonymous inner class is an inner class without a name  Used to simplify code needed to handle events  See pp. 336 10

25 Chapter 10 - Writing Graphical User Interfaces25 Creating Inner Classes

26 Chapter 10 - Writing Graphical User Interfaces26 Creating Inner Classes

27 Chapter 10 - Writing Graphical User Interfaces27 Creating Inner Classes  When you compile an inner class – the compiler produces two class files  If your outer class is named Outer and your inner class is named Inner – a filed called Outer$Inner.class is created  If the inner class is anonymous, the class file for the inner class has the same name as the outer class with a number beginning with 1 concatenated after the $ 10

28 Chapter 10 - Writing Graphical User Interfaces28 Converting an Application to an Applet  An applet is a class you write that extends the Applet class  Runs under the control of a web browser  Can convert AWTFrameAndComponent to an applet by:  Delete the main method  Delete all references to close button  Delete the anonymous inner class  Delete the shutDown method  Delete the setVisible, setSize, and setTitle methods

29 Chapter 10 - Writing Graphical User Interfaces29 Converting an Application to an Applet

30 Chapter 10 - Writing Graphical User Interfaces30 Using Swing Classes  Swing classes are newer improved versions of the original AWT classes  Want to use for there improved appearance and capability

31 Chapter 10 - Writing Graphical User Interfaces31 Converting AWT GUI to Swing  Swing components reside in javax.swing: import javax.swing.*;  Change class names of Button, Label, TextField, and Panel to JButton, JLabel, JTextField, and JPanel  Use JFrame instead of Frame  Compare Figure 10-17 vs. Figure 10-14 10

32 Chapter 10 - Writing Graphical User Interfaces32 Converting AWT GUI to Swing 10 AWT

33 Chapter 10 - Writing Graphical User Interfaces33 Adding Drop-down Menus  Swing drop down menus consist of three classes  JMenuBar  JMenu  JMenuItem  See Figure 10-20 on page 352 10

34 Chapter 10 - Writing Graphical User Interfaces34 Adding Drop-down Menus 10

35 Chapter 10 - Writing Graphical User Interfaces35 Converting a Swing Application to an Applet  Steps for conversion include:  Extend JApplet instead of JFrame  Delete the main method  Delete all references to close menu item  Delete the anonymous inner class  Delete the shutDown method  Delete the statements setVisible, setSize, and setTitle

36 Chapter 10 - Writing Graphical User Interfaces36 Converting a Swing Application to an Applet


Download ppt "Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces."

Similar presentations


Ads by Google