




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















-^ Program
-^ Statements
execute
in
sequential,
predetermined
order
-^ Typically
use
keyboard
or
file
but
program
determines
when
that
happens • Usually
single
‐threaded
-^ Event
-^ Program
waits
for
user
input
to
activate
certain
statements
-^ Typically
uses
a^
(Graphical
User
Interface) • Often
multi
‐threaded
-^
-^
-^
-^
-^ Classes
-^ Major
-^ awt
and
swing
-^ Pluggable
look
‐and
‐feel
support
-^ Accessibility
-^ Java
-^ Drag
‐and
‐drop
Support
-^ Internationalization
-^
-^
-^
-^
-^
-^
Why
Swing?
-^
-^
Statics:
what’s
drawn
on
the
screen •^ Components
-^ buttons,
labels,
lists,
sliders,
menus,
...
-^ Containers:
components
that
contain
other
components
-^ frames,
panels,
dialog
boxes,
...
-^ Layout
managers:
control
placement
and
sizing
of
components
Dynamics: user interactions •^
Events
-^
button
‐press,
mouse
‐click,
key
‐
press,
...
-^
Listeners:
an
object
that
responds
to
an
event
-^
Helper
classes
-^
Graphics,
Color,
Font,
FontMetrics,
Dimension,
...
import
javax.swing.;*
public
class
Basic
public
static
void
main(String[]
args)
//create
the
window
JFrame
f
new
JFrame("Basic
Test!");
//quit
Java
after
closing
the
window
f.setDefaultCloseOperation(JFrame.
f.setSize(200,
//set
size
in
pixels
f.setVisible(true);
//show
the
window
A
More
Extensive
Example
^
import
javax.swing.;*
^
import
java.awt.;*
^
import
java.awt.event.;*
^
public
class
Intro
extends
JFrame
{
^
private
int
count
=
0;
^
private
JButton
myButton
=^
new
JButton("Push
Me!");
^
private
JLabel
label
=
new
JLabel("Count:
"
+^
count);
^
public
Intro()
{
^
setDefaultCloseOperation(
EXIT_ON_CLOSE
);
^
setLayout(new
FlowLayout(FlowLayout.
LEFT
));
//set
layout
manager
^
add(myButton);
//add
components
^
add(label);
^
myButton.addActionListener(new
ActionListener()
{
^
public
void
actionPerformed(ActionEvent
e)
{
^
count++;
^
label.setText("Count:
"^
+^
count);
^
}
^
});
^
pack();
^
setVisible(true);
^
} ^
public
static
void
main(String[]
args)
{
^
try
{
^
UIManager.
setLookAndFeel
(UIManager.
getSystemLookAndFeelClassName
());
^
}^ catch
(Exception
exc)
{}
^
new
Intro();
^
} ^
}
-^
import javax.swing.;*
-^
import java.awt.;*
-^
public class ComponentExamples extends JFrame {
-^
public ComponentExamples() {
-^
setLayout(new FlowLayout(FlowLayout.
LEFT
));
-^
add(new JButton("Button"));
-^
add(new JLabel("Label"));
-^
add(new JComboBox(new String[] { "A", "B", "C" }));
-^
add(new JCheckBox("JCheckBox"));
-^
add(new JSlider(0, 100));
-^
add(new JColorChooser());
-^
setDefaultCloseOperation(
EXIT_ON_CLOSE
);
-^
pack();
-^
setVisible(true);
-^
}
-^
public static void main(String[] args) {
-^
try {
-^
UIManager.
setLookAndFeel
(UIManager.
getSystemLookAndFeelClassName
());
-^
} catch (Exception exc) {}
-^
new ComponentExamples();
-^
}
-^
}
Can
hold
other
components Has
a
layout
manager
A^
heavyweight
component
interacts
directly
with
the
host
system
JWindow,
JFrame,
and
JDialog
are
heavyweight
Except
for
these
top
‐level
containers,
Swing
components
are
almost
all
lightweight^
JPanel
is
lightweight
: top-level window with no
border JFrame
: top-level window with border
and (optional) menu bar JDialog
: used for dialog windows
: used mostly to organize
objects within other containers
JComboBox (mi)
JComboBox (km)
JTextField (2000)
JSlider
JTextField (3226)
JSlider