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.
21Implementing Interfaces
22Extending 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
23Extending Adapter Class
24Creating Inner Classes
10
- 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
25Creating Inner Classes
26Creating Inner Classes
27Creating Inner Classes
10
- 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
OuterInner.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
28Converting 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
29Converting an Application to an Applet
30Using Swing Classes
- Swing classes are newer improved versions of the
original AWT classes - Want to use for there improved appearance and
capability
31Converting 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
- Compare Figure 10-17 vs. Figure 10-14
32Converting AWT GUI to Swing
10
AWT
33Adding Drop-down Menus
10
- Swing drop down menus consist of three classes
- JMenuBar
- JMenu
- JMenuItem
- See Figure 10-20 on page 352
34Adding Drop-down Menus
10
35Converting 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
36Converting a Swing Application to an Applet