TCSS 143, Autumn 2004 Lecture Notes: Graphical User Interfaces Koffman/Wolfgang Appendix C, Pp. 747-786
TCSS 143, Autumn 2004 Lecture Notes: Graphical User Interfaces Koffman/Wolfgang Appendix C, Pp. 747-786
Lecture Notes
Graphical User Interfaces
Koffman/Wolfgang Appendix
C,
pp. 747-786
1
Motivation
Simplest GUI
programming:
JOptionPane
advantages:
simple
flexible (in some ways)
looks better than the black box of doom
disadvantages:
Types of JOptionPanes
JOptionPane examples 1
import javax.swing.*;
class MessageDialogExample {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
"How's the weather?");
JOptionPane.showMessageDialog(null,
"Second message");
}
}
JOptionPane examples 2
import javax.swing.*;
class ConfirmDialogExample {
public static void main(String[] args) {
int choice = JOptionPane.showConfirmDialog(null,
"Erase your hard disk?");
if (choice == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Disk erased!");
} else {
JOptionPane.showMessageDialog(null, "Cancelled.");
}
}
}
JOptionPane examples 3
import javax.swing.*;
class InputDialogExample {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null,
"What's yer name, pardner?");
JOptionPane.showMessageDialog(null, "Yeehaw, " + name);
}
}
Swing component
hierarchy
java.lang.Object
+--java.awt.Component
+--java.awt.Container
|
+--javax.swing.JComponent
|
+--javax.swing.JButton
|
+--javax.swing.JLabel
|
+--javax.swing.JMenuBar
|
+--javax.swing.JOptionPane
|
+--javax.swing.JPanel
|
+--javax.swing.JTextArea
|
+--javax.swing.JTextField
|
+--java.awt.Window
+--java.awt.Frame
+--javax.swing.JFrame
import java.awt.*;
import javax.swing.*;
10
JFrame
A frame is a graphical window that can
be used to hold other components
public JFrame()
or
public JFrame(String title)
Creates a frame with an optional title.
JButton, JLabel
The most common component
a button is a clickable onscreen
region that the user interacts with
to perform a single command
A text label is simply a string of text
displayed on screen in a graphical
program. Labels often give information or describe other components
14
JTextField, JTextArea
A text field is like a label, except that the text
in it can be edited and modified by the user.
Text fields are commonly used for user input,
where the user types information in the field
and the program reads it
A text area is a multi-line text field
15
JCheckBox, JRadioButton
A check box is a toggleable button with two states:
checked and unchecked
A radio button is a button that can be selected; usually part of a
group of mutually-exclusive radio buttons (1 selectable at a time)
16
ButtonGroup
A logical group of radio buttons that ensures
that only one is selected at a time
public ButtonGroup()
public void add(JRadioButton button)
17
Problem: positioning,
resizing
How does the programmer specify where each
19
Container
A container is an object that holds components; it also
governs their positions, sizes, and resize behavior
JPanel
A panel is our container of choice; it inherits the
methods from the previous slide and defines
these additional methods (among others):
public JPanel()
Constructs a panel with a default flow layout.
21
Preferred size of
components
Swing component objects each have a certain size they
would "like" to be--just large enough to fit their contents
(text, icons, etc.)
This is called the preferred size of the component
Some types of layout managers (e.g. FlowLayout)
choose to size the components inside them to the
preferred size; others (e.g. BorderLayout, GridLayout)
disregard the preferred size and use some other
scheme
22
BorderLayout
public BorderLayout()
23
FlowLayout
public FlowLayout()
24
GridLayout
public GridLayout(int rows, int columns)
25
BoxLayout
Box.createHorizontalBox()
Box.createVerticalBox()
Other layouts
CardLayout
layers of "cards" stacked
on top of each other;
one visible at a time
GridBagLayout
very complicated;
my recommendation:
never ever use it
28
29
What is missing?
Event-driven Programming
import java.awt.event.*;
32
Action events
(
ActionEvent)
most common / simple event type in Swing
represent an action occurring on a GUI
component
created by:
button clicks
check box checking / unchecking
menu clicks
pressing Enter in a text field
etc.
33
34
Writing an ActionListener
// part of Java; you dont write this
public interface ActionListener {
void actionPerformed(ActionEvent event);
}
// Prints a message when the button is clicked.
public class MyActionListener
implements ActionListener {
public void actionPerformed(ActionEvent event){
System.out.println("Event occurred!");
}
}
35
Attaching an
ActionListener
JButton button = new JButton("button
1");
ActionListener listener = new MyActionListener();
button.addActionListener(listener);
36
ActionEvent objects
38
...
}
}
);
}
}
39
ActionListener in JFrame
public class Outer extends JFrame
implements ActionListener {
public Outer() {
JButton myButton = new JButton();
myButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
...
}
}
40
References