Title: Chapter 10 Writing Graphical User Interfaces
1Chapter 10Writing Graphical User Interfaces
2Understanding Javas GUI Classes
10
- 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
3Understanding Javas GUI Classes
10
4Understanding Javas GUI Classes
5Understanding Javas GUI Classes
10
- 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
6Understanding Javas GUI Classes
10
7Using 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
8Creating 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 classpublic class AWTFrameWithButton
extends Frame
9Creating a Window with a Button
10
- Constructor must accomplish five tasks
- Create and instance of Button
- Add the button to the frame
- Establish the frame size
- Place a title on the frame
- Make the frame visible
10Creating a Window with a Button
11Using 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
12Using Layout Managers
10
- 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
13Using Layout Managers
14Using 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
15Using Layout Managers
16Handling 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.
17Handling Java Events
10
18Handling 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
19Handling Java Events
- There are three methods for handling events
- Implementing interfaces
- Extending Adapter classes
- Creating Inner classes
20Implementing 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 interfaces methods
and deal with the events - Interface for buttons is ActionListener
- Need to import interface import
java.awt.event.
21Event Handling using listener interfaces (i)
22Event Handling using listener interfaces (ii)
23Implementing Interfaces
24Extending Adapter Class
- Supplied adapter class that implements a listener
interface, and then overrides all of the
interface methods with null methods - Must override methods you want to respond to
- Need to create two classes one that extends
Frame, and another that extends WindowAdapter
25Extending Adapter Class
26Event handling using Adapter Class (i)
27Event handling using Adapter Class (ii)
28Creating Inner Classes
10
- Third way of dealing with events is to use an
anonymous inner class - Used to simplify code needed to handle events
29Creating Inner Classes
30Creating Inner Classes
31Creating Inner Classes
10
- When you compile an inner class the compiler
produces two class files - The class file for the anonymous inner class has
the same name as the outer class with a number
beginning with 1 concatenated after the , for
example, ATWFrame1.java
32Event handling using an Anonymous Inner class (i)
33Event handling using an Anonymous Inner class (ii)
34Multiple AWT components (i)
35Multiple AWT components (ii)
36Converting 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
37AWT Applet (i)
38AWT Applet (ii)
39Converting an Application to an Applet
40Using Swing Classes
- Swing classes are newer improved versions of the
original AWT classes - Want to use for there improved appearance and
capability
41Converting AWT GUI to Swing
10
- 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
42Swing Components (i)
43Swing Components (ii)
44Converting AWT GUI to Swing
10
45Adding Drop-down Menus
10
- Swing drop down menus consist of three classes
- JMenuBar
- JMenu
- JMenuItem
46Adding Drop-down Menus
10
47Swing Menus (i)
48Swing Menus (ii)
49Converting 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
50Swing Applet (i)
51Swing Applet (ii)
52Converting a Swing Application to an Applet