Professional Documents
Culture Documents
28-GUI Application
28-GUI Application
28-GUI Application
PLAN
o GUI
o GUI Components
o Event Handler
2
Introduction
3
Introduction (cont.)
4
GUI Example
5
Overview of Swing Components
6
Overview of Swing Components
7
Using JOptionPane for Output
import javax.swing.*;
. . .
8
Using JOptionPane for Output - 2
import javax.swing.*;
. . .
9
JOptionPane for Input
import javax.swing.*;
. . .
String inputstr =
10
JOptionPane static constants
11
Displaying Text and Images in a Window
o Most windows that can contain Swing GUI components are instances of
class JFrame or a subclass of JFrame.
o JFrame is an indirect subclass of class java.awt.Window
o Provides the basic attributes and behaviors of a window
– a title bar at the top
– buttons to minimize, maximize and close the window
o Most of our examples will consist of two classes
– a subclass of JFrame that demonstrates new GUI concepts
– an application class in which main creates and displays the
application’s primary window.
12
Subclassing JFrame
13
Creating a Plain JFrame
import javax.swing.*;
class DefaultJFrame {
public static void main( String[] args ) {
JFrame defaultJFrame;
defaultJFrame = new JFrame();
defaultJFrame.setVisible(true);
}
}
14
Creating a Subclass of JFrame
import javax.swing.*;
15
The Content Pane of a Frame
16
Changing the Background Color
17
Placing GUI Objects on a Frame
18
Placing a Button
19
IDE Help for GUI Desgin
o Many IDEs provide GUI design tools in which you can specify the
exact size and location of a component
o IDE generates the GUI code for you
o Greatly simplifies GUI creation
o To ensure that this book’s examples can be used with any IDE, we
did not use an IDE to create the GUI code
o We use Java’s layout managers in our GUI examples
20
Layout Managers
21
FlowLayout
22
FlowLayout Sample
23
BorderLayout
o This layout manager divides the container into five regions: center,
north, south, east, and west.
o The north and south regions expand or shrink in height only
o The east and west regions expand or shrink in width only
o The center region expands or shrinks on both height and width.
o Not all regions have to be occupied.
24
BorderLayout Sample
25
GridLayout
26
GridLayout Sample
27
Event Handling
28
Event Source Objects
29
Event Listener Objects
30
Connecting Source and
Listener
event source event listener
notify
JButton Handler
register
31
Event Types
32
Handling Action Events
action event action event
source
actionPerformed
listener
JButton Button
Handler
addActionListener
JButton button = new JButton("OK");
ButtonHandler handler = new ButtonHandler( );
button.addActionListener(handler);
33
The Java Interface
34
ActionListener Interface
35
The ButtonHandler Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
36
Container as Event Listener
37
ButtonFrameHandlerDemo
. . .
class JButtonFrameHandlerDemo extends JFrame implements ActionListener {
. . .
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
38
GUI Classes for Handling Text
39
JTextField
40
JLabel
41
Ch14TextFrame2
JLabel
(with a text)
JLabel
(with an image)
JTextField
42
JTextArea
44
Adding Scroll Bars to
JTextArea
o By default a JTextArea does not have any scroll bars. To add
scroll bars, we place a JTextArea in a JScrollPane object.
45
Demo
46
Other Common GUI Components
• JCheckBox
• JRadioButton
• JComboBox
• JList
• JSlider
47
Menus
48
Menu Components
Edit View Help
JMenu
JMenuItem
separator
49
Sequence for Creating Menus
50
Demo
51