Professional Documents
Culture Documents
Chapter 1 Intro To GUI - Part 1
Chapter 1 Intro To GUI - Part 1
Refer to:
Course Information for Students
Lesson Objectives
Introduction
A graphical user interface (GUI)
makes a system user-friendly and
easy to use.
Dialog Boxes
Also known as dialogs.
Windows in which programs display
important messages to the user or
obtain information from the user.
Javas javax.swing.JOptionPane
class provides pre-built dialog boxes
for input and output.
import javax.swing.JOptionPane;
public class Addition {
public static void main(String[] args) {
String firstNumber = JOptionPane.showInputDialog("Enter first integer");
String secondNumber = JOptionPane.showInputDialog("Enter second
integer");
int firstInt = Integer.parseInt(firstNumber);
int secondInt = Integer.parseInt(secondNumber);
int sum = firstInt + secondInt;
JOptionPane.showMessageDialog(null, "The sum is " + sum);
}
}
9
JOptionPane Dialogs
A dialog is normally used as a temporary
window to receive additional information
from the user, or to provide notification
that some event has occurred.
A JOptionPane
dialog can display
an icon, a message,
an input, and option
buttons.
10
Message Dialogs
A message dialog box simply displays a
message to alert the user and waits for the user
to click the OK button to close the dialog.
JOptionPane.showMessageDialog(null, This is an error",
Error", JOptionPane.INFORMATION_MESSAGE);
11
Message Types
The messageType is one of the following
constants:
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.PLAIN_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
Input Dialogs
An input dialog box is used to receive input
from the user.
13
Confirmation Dialogs
A message dialog box displays a message
and waits for the user to click the OK button
to dismiss the dialog. The message dialog
does not return any value. A confirmation
dialog asks a question and requires the user
to respond with an appropriate button. The
confirmation dialog returns a value that
corresponds to a selected button.
14
Good UI Design
Consistency.
Consistent user interfaces enable a user
to learn new applications faster.
Use sentence-style capitalization for
prompts in an input dialog.
Use book-title capitalization for the title
bar of a window.
15
P1 Q1
16
Modal Dialog
Each JOptionPane dialog that you display
is a modal dialog while the dialog is on
the screen, the user cannot interact with
the rest of the application.
Do not overuse modal dialogs as they can
reduce the usability of your applications.
Use them only when its necessary to
prevent users from interacting with the
rest of the application until they dismiss
the dialog.
17
Introduction to GUI
Components
18
GUI Components
A.k.a. controls or widgets (window
Frames
gadgets).Windows that can include a title bar, menu
Coming up next
The following section provides a brief
overview of the Java GUI API.
A detailed coverage will be covered
much later.
20
Package
javax.swing
java.awt
java.awt.event
Type of Classes
GUI components such as labels, text
fields, buttons, etc. When you use a
Swing component, you work with
objects of that components class. You
create the component by calling its
constructor and then call methods of
the component as needed for proper
setup.
The Abstract Windowing Toolkit
Event-handling classes that handle
user input.
21
Compone
nt classes
Container
classes
Helper
classes
22
Swing Components
All Swing components are subclasses
of the abstract class JComponent.
Jcomponent include methods to
o Set the size of a component
o Change the background color
o Define the font used for any displayed text
o Set up ToolTips explanatory text that
appears when the mouse pointer is over
the component.
23
LayoutManager
1
Heavyweight
FontMetrics
Object
Color
Panel
Applet
JApplet
Window
Frame
JFrame
Dialog
JDialog
Graphics
Component
Container
Swing Components
in the javax.swing package
JComponent
Lightweight
24
Component Classes
An instance of Component can be
displayed on the screen.
25
AbstractButton
JComponent
JMenuItem
JMenu
JButton
JRadioButtonMenuItem
JToggleButton
JCheckBox
JRadioButton
JEditorPane
JTextComponent
JTextField
JPasswordField
JTextArea
JLabel
JTabbedPane
JToolBar
JTree
JComboBox
JList
JSplitPane
JMenuBar
JTable
JPanel
JLayeredPane
JPopupMenu
JTableHeader
JOptionPane
JSeparator
JFileChooser
JInternalFrame
JScrollBar
JSlider
JScrollPane
JRootPane
JColorChooser
JProgressBar
26
JToolTip
JSpinner
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-font: java.awt.Font
-background: java.awt.Color
-foreground: java.awt.Color
-preferredSize: Dimension
-visible: boolean
+getWidth(): int
+getHeight(): int
+getX(): int
+getY(): int
java.awt.Container
+add(comp: Component): Component
+add(comp: Component, index: int): Component Adds a component to the container with the specified index.
Removes the component from the container.
+remove(comp: Component): void
+getLayout(): LayoutManager
javax.swing.JComponent
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
-toolTipText: String
The tool tip text for this component. Tool tip text is displayed when
the mouse points on the component without clicking.
-border: javax.swing.border.Border
27
Label
Text Check
field Box
Radio
Button
Button
Combo
Box
Container Classes
An instance of Container can hold
instances of Component.
29
Container Classes
Dimension
Font
LayoutManager
1
Heavyweight
FontMetrics
Object
Color
Panel
Applet
JApplet
Window
Frame
JFrame
Dialog
JDialog
Graphics
Component
Container
JComponent
Swing Components
in the javax.swing package
JPanel
Lightweight
30
javax.swing.JFrame
javax.swing.JPanel
javax.swing.JApplet
javax.swing.JDialog
Description
Used to group components.
Frames, panels, and applets are
its subclasses.
A window not contained inside
another window. Used to hold
other GUI components.
An invisible container that holds
UI components. Panels can be
nested.
A base class for creating a Java
applet.
A popup window or message box
used as a temporary window to
receive additional31info or provide
notification that an event has
LayoutManager
1
Heavyweight
FontMetrics
Object
Color
Panel
Applet
JApplet
Window
Frame
JFrame
Dialog
JDialog
Graphics
Component
Container
JComponent
Swing Components
in the javax.swing package
JPanel
Lightweight
32
Description
Deals with colors of GUI
components, e.g. to specify
background or foreground
colors.
java.awt.Font
Specifies fonts for the text.
java.awt.FontMetrics
An abstract class used to get
the properties of the fonts.
java.awt.Dimension
Encapsulates the width and
height of a component in a
single object.
java.awt.LayoutManager Specifies how components are
arranged in a container
33
Review: What Am I?
1. An object that can be displayed on the
screen.
A component
34
Review
1. The GUI API classes may be classified
into 3 groups. What are the 3 groups?
Component classes
Container classes
Helper classes
35
36
Frames
A window for holding other GUI
components. It is a window that is not
contained inside another window.
To create a frame, use the JFrame.
37
JFrame Class
javax.swing.JFrame
+JFrame()
+JFrame(title: String)
+setLocationRelativeTo(c: Component):
void
+pack(): void
38
39
import javax.swing.JOptionPane;
public class MyFrame extends JFrame {
public MyFrame() {
super("Frame Title");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}
40
41
JFrame Class
javax.swing.JFrame
+JFrame()
+JFrame(title: String)
+setLocationRelativeTo(c: Component):
void
+pack(): void
42
HIDE_ON_CLOSE
44
Creating a Component
Each GUI component is represented
by its own class. To create an GUI
component in Java, you create an
object of that components class.
E.g., to create a button, just create an
instance of JButton.
45
46
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame {
private JButton jbtOK = new JButton("OK");
public MyFrame() {
super("Frame Title");
add(jbtOK);
...
}
...
}
47
JButton Constructors
The following are JButton constructors:
JButton()
JButton(String text)
JButton(String text, Icon icon)
JButton(Icon icon)
48
Adding Components to a
Container
To add a component to a container, call
the containers add(Component)
method with the component as the
argument
(all GUI components in Swing inherit
from java.awt.Component).
49
P1 Q2
50