Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Method Overloading
Review
Example: java.lang.Math
static method max takes in
two numbers and returns the
greater of the two
10/9/2014
A String (java.lang.String)
is a sequence of alphanumeric
characters, including space!
Example:
System.out.println(s);
public Wardrobe() {
_top = T-Shirt;
_bottom = Jeans;
}
Lecture 9
Intro to Swing
0 of 95
10/9/2014
What is Swing?
JFrame
JPanel
JButtons
2 of 95
(0, 0)
o corresponds to Latin-based
languages writing order
3 of 95
pixels
4 of 95
10/9/2014
JLabel
Hello, World!
JButtons
5 of 95
JPanel
Add JButtons,
JLabels, etc. to JPanel
pixels
JFrame
6 of 95
JFrame
JPanel
JLabel
Hello, World!
JButtons
JLabel
JPanel
JButton
8 of 95
10/9/2014
DEMO: ColorTextApp
Process
JFrame
JPanel
JButton
JFrame
JLabel
JPanel
JButton
10 of 95
public ColorTextApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
11 of 95
public ColorTextApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
First we instantiate a
JFrame, which we store in
local variable frame
frame.setVisible(true);
12 of 95
10/9/2014
frame.setVisible(true);
public ColorTextApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
13 of 95
Process
1. Create a top-level App class that
contains an instance of JFrame
JFrame
14 of 95
JLabel
JPanel
The class
ColorTextPanel is a
JPanel: our canvas that
may contain other Swing
GUI components
Weve specialized this
class to contain and
display a JLabel and a
Jbutton (local vars)
JButton
16 of 95
10/9/2014
public ColorTextPanel() {
JLabel label = new JLabel("CS15 Rocks!");
JButton button = new JButton("Random Color");
17 of 95
Process
1. Create a top-level App class that
contains an instance of JFrame
18 of 95
19 of 95
JFrame
JLabel
JPanel
JButton
10/9/2014
public ColorTextApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
Process
frame.setVisible(true);
JFrame
JLabel
JPanel
JButton
21 of 95
ActionListeners
24 of 95
10/9/2014
ActionListeners
JButton
ActionListener
ActionEvent e
Click!
@Override
public void actionPerformed(ActionEvent e) {
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color random = new Color(red, green, blue);
_label.setForeground(random);
}
25 of 95
26 of 95
actionPerformed defines
response to ActionEvent
@Override
public void actionPerformed(ActionEvent e) {
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color random = new Color(red, green, blue);
_label.setForeground(random);
}
27 of 95
@Override
public void actionPerformed(ActionEvent e) {
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color random = new Color(red, green, blue);
_label.setForeground(random);
}
28 of 95
10/9/2014
Use Math.random() to
generate three random ints
0-255
Use them as RGB values
for a new Color
Call JLabel method
setForeground on _label
to set its color to the random
color weve created
}
Adding ActionListeners
@Override
public void actionPerformed(ActionEvent e) {
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color random = new Color(red, green, blue);
_label.setForeground(random);
}
29 of 95
30 of 95
Process
JFrame
public ColorTextApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JLabel
JPanel
JButton
}
31 of 95
public ColorTextPanel() {
JLabel label = new JLabel("CS15 Rocks!");
JButton button = new JButton("Random Color");
ColorListener listener = new ColorListener(label);
button.addActionListener(listener);
Dimension panelSize = new Dimension(150, 75);
this.setPreferredSize(panelSize);
this.add(label);
this.add(button);
}
frame.setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color random = new Color(red, green, blue);
_label.setForeground(random);
}
32 of 95
10
10/9/2014
ColorTextApp
ColorTextApp
ColorTextPanel
JFrame
JLabel
ColorTextPanel
JButton
JLabel
JFrame
JButton
JPanel
JButton
Graphically, the ColorTextPanel is contained within the JFrame, but logically, both are
contained together in a top-level App class; App and ColorListener arent graphic!
Logical containment is based on where objects are created, while graphical is based on swing
elements being added to other swing elements via the add() method
Note: The JButton also has a reference to ColorListener, but this reference is omitted from our
diagram to avoid excessive complexity.
34 of 95
DEMO: Clock
ColorListener
ColorListener
33 of 95
JLabel
JFrame
JPanel
JLabel
35 of 95
36 of 95
11
10/9/2014
Using Timers
Using Timers
The class javax.swing.Timer comes in handy when we
need to perform a task repeatedly at regular intervals
When we instantiate a Timer, we pass it:
o
o
ActionListener
ActionEvent e
Timer
Tick!
38 of 95
37 of 95
Process: Clock
1. Create top-level class that
contains a JFrame
JFrame
public Clock() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
As in the previous
example, we create a toplevel class that contains a
JFrame
JPanel
JLabel
}
39 of 95
frame.pack();
frame.setVisible(true);
12
10/9/2014
Process: Clock
public Clock() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Call setPreferredSize on
panel, passing in the
Dimension we created
JLabel
42 of 95
public ClockPanel() {
Dimension panelSize =
new Dimension(250, 50);
this.setPreferredSize(panelSize);
public ClockPanel() {
Dimension panelSize =
new Dimension(250, 50);
this.setPreferredSize(panelSize);
JPanel
As in previous example,
create a Dimension to
represent desired width and
height of panel
JFrame
43 of 95
44 of 95
13
10/9/2014
Process: Clock
frame.pack();
frame.setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
long currTime = System.currentTimeMillis();
Date now = new Date(currTime);
_timeLabel.setText(now.toString());
}
47 of 95
JFrame
JPanel
JLabel
46 of 95
System.currentTimeMillis
returns number of
milliseconds elapsed since
midnight January 1, 1970
Can convert this number to
the actual date and time by
passing it into a new
java.util.Date
@Override
public void actionPerformed(ActionEvent e) {
long currTime = System.currentTimeMillis();
Date now = new Date(currTime);
_timeLabel.setText(now.toString());
}
48 of 95
14
10/9/2014
@Override
public void actionPerformed(ActionEvent e) {
long currTime = System.currentTimeMillis();
Date now = new Date(currTime);
_timeLabel.setText(now.toString());
}
49 of 95
Instantiating a TimerListener
In constructor of
ClockPanel class, we
instantiate a new
TimerListener
JPanel
JLabel
50 of 95
JFrame
Process: Clock
}
51 of 95
TimerListener listener =
new TimerListener(timeLabel);
_timer = new Timer(100, listener);
_timer.start();
}
52 of 95
15
10/9/2014
Clock
}
}
public class TimerListener
implements ActionListener {
private JLabel _timeLabel;
frame.pack();
frame.setVisible(true);
JFrame
ClockPanel
@Override
public void actionPerformed(ActionEvent e) {
long currTime = System.currentTimeMillis();
Date now = new Date(currTime);
_timeLabel.setText(now.toString());
}
JLabel
TimerListener
53 of 95
Timer
54 of 95
DEMO: ColorPanelApp
Specification: An app
with three sliders that
the user can drag to
change the RGB values
of the background color
Useful classes: JFrame,
JPanel, JSlider,
ChangeListener
JPanel
JSlider
55
of 95
62/102
56
of 95
63/102
16
10/9/2014
ChangeListeners
Using JSliders
A JSlider lets the user graphically select a value by
sliding an arrow within a bounded interval
ChangeListener
ChangeEvent e
JSlider
58
of 95
65/102
64/102
Process: ColorPanelApp
JFrame
JPanel
public ColorPanelApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JSlider
}
59
of 95
66/102
frame.pack();
frame.setVisible(true);
17
10/9/2014
Process: ColorPanelApp
JFrame
ColorPanel is a
JPanel
JPanel
JSlider
public ColorPanel() {
Dimension panelSize = new Dimension(300, 150);
this.setPreferredSize(panelSize);
this.setBackground(Color.GRAY);
JSlider sliderRed = new JSlider(0, 255);
JSlider sliderGreen = new JSlider(0, 255);
JSlider sliderBlue = new JSlider(0, 255);
62
of 95
69/102
Arguments are
beginning/end of
sliders range: we give
each range 0-255 (one
byte/RGB channel)
this.add(sliderRed);
this.add(sliderGreen);
this.add(sliderBlue);
61
of 95
68/102
public ColorPanel() {
Dimension panelSize = new Dimension(300, 150);
this.setPreferredSize(panelSize);
this.setBackground(Color.GRAY);
JSlider sliderRed = new JSlider(0, 255);
JSlider sliderGreen = new JSlider(0, 255);
JSlider sliderBlue = new JSlider(0, 255);
As in other examples,
create a Dimension,
give it desired width
and height, then call
setPreferredSize
this.add(sliderRed);
this.add(sliderGreen);
this.add(sliderBlue);
63
of 95
70/102
public ColorPanelApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
Just as we did in
previous examples,
first instantiate a panel,
then call add to add it
to the JFrame
frame.pack();
frame.setVisible(true);
18
10/9/2014
Process: ColorPanelApp
JFrame
When red sliders listener detects that its slider has been moved:
o it calls getBackground to ask the panel for its current color
JPanel
JSlider
65
of 95
72/102
67
of 95
74/102
66
of 95
73/102
@Override
public void stateChanged(ChangeEvent e) {
// implementation elided for now
}
@Override
public void stateChanged(ChangeEvent e) {
// implementation elided for now
}
68
of 95
75/102
19
10/9/2014
switch(_channel){
case 0:
newbg = new Color(val, bg.getGreen(),
bg.getBlue());
break;
case 1:
newbg = new Color(bg.getRed(), val,
bg.getBlue());
break;
default:
newbg = new Color(bg.getRed(), bg.getGreen(),
val);
}
_panel.setBackground(newbg);
69
of 95
76/106
switch(_channel){
case 0:
newbg = new Color(val, bg.getGreen(),
We declare a new Color, newBg,
bg.getBlue());
break;
to which well assign a different
case 1:
value based on which color
newbg = new Color(bg.getRed(), val,
bg.getBlue());
channel the listener is supposed
break;
to change
default:
newbg = new Color(bg.getRed(), bg.getGreen(),
val);
}
_panel.setBackground(newbg);
71
of 95
78/106
}
switch(_channel){
case 0:
newbg = new Color(val, bg.getGreen(),
bg.getBlue());
break;
case 1:
newbg = new Color(bg.getRed(), val,
bg.getBlue());
break;
default:
newbg = new Color(bg.getRed(), bg.getGreen(),
val);
}
_panel.setBackground(newbg);
70
of 95
77/106
switch(_channel){
case 0:
newbg = new Color(val, bg.getGreen(),
bg.getBlue());
break;
case 1:
newbg = new Color(bg.getRed(), val,
bg.getBlue());
break;
default:
newbg = new Color(bg.getRed(), bg.getGreen(),
val);
}
_panel.setBackground(newbg);
72
of 95
79/106
20
10/9/2014
Process: ColorPanelApp
Back in ColorPanel
class
JFrame
Call method
addChangeListener on
each slider, passing in a
new instance of
SliderListener
JPanel
JSlider
73
of 95
80/106
Pass in ColorPanel,
appropriate channel
number and slider as
arguments to each
SliderListener
public ColorPanel() {
Dimension panelSize = new Dimension(300, 150);
this.setPreferredSize(panelSize);
this.setBackground(Color.GRAY);
JSlider sliderRed = new JSlider(0, 255);
JSlider sliderGreen = new JSlider(0, 255);
JSlider sliderBlue = new JSlider(0, 255);
sliderRed.addChangeListener(
new SliderListener(this, 0, sliderRed));
sliderGreen.addChangeListener(
new SliderListener(this, 1, sliderGreen));
sliderBlue.addChangeListener(
new SliderListener(this, 2, sliderBlue));
this.add(sliderRed);
this.add(sliderGreen);
this.add(sliderBlue);
}
74
of 95
81/106
public ColorPanel() {
Dimension panelSize = new Dimension(300, 150);
this.setPreferredSize(panelSize);
this.setBackground(Color.GRAY);
JSlider sliderRed = new JSlider(0, 255);
JSlider sliderGreen = new JSlider(0, 255);
JSlider sliderBlue = new JSlider(0, 255);
public ColorPanelApp() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
ColorPanel panel = new ColorPanel();
frame.add(panel);
sliderRed.addChangeListener(
new SliderListener(this, 0, sliderRed));
sliderGreen.addChangeListener(
new SliderListener(this, 1, sliderGreen));
sliderBlue.addChangeListener(
new SliderListener(this, 2, sliderBlue));
frame.pack();
frame.setVisible(true);
this.add(sliderRed);
this.add(sliderGreen);
this.add(sliderBlue);
75
of 95
82/106
@Override
public void stateChanged(ChangeEvent e) {
int val = _slider.getValue();
Color bg = _panel.getBackground();
Color newbg;
switch(_channel){
case 0:
newbg = new Color(val, bg.getGreen(),
bg.getBlue());
break;
case 1:
newbg = new Color(bg.getRed(), val,
bg.getBlue());
break;
default:
newbg = new Color(bg.getRed(), bg.getGreen(),
val);
}
_panel.setBackground(newbg);
}
76
of 95
83/106
21
10/9/2014
Buttons in Swing
ColorPanelApp
JFrame
ColorPanel
JSlider
SliderListener
77
of 95
84/106
78
of 95
85/106
ButtonGroups
Buttons in Swing
79
of 95
86/106
80
of 95
87/106
22
10/9/2014
create buttons
create ButtonGroup
81
of 95
88/106
82
of 95
89/106
83
of 95
90/106
84
of 95
91/106
23
10/9/2014
FlowLayout
Using java.awt.LayoutManagers
Each JPanel contains a LayoutManager to
automatically arrange its sub-components
java.awt.FlowLayout
java.awt.GridLayout
java.awt.BorderLayout
Arranges components
from left to right, top to
bottom
FlowLayout
86
of 95
93/106
GridLayout
87
of 95
94/106
Arranges components in
a grid
Add components left to
right, top to bottom
Must specify grid size in
constructor-- number of
rows and columns
Each cell in grid is same
size, determined by
largest element in grid
95
of 95
95/106
24
10/9/2014
BorderLayout
GridLayout
p1.setBackground(Color.GREEN);
p2.setBackground(Color.RED);
p3.setBackground(Color.ORANGE);
p4.setBackground(Color.CYAN);
p5.setBackground(Color.BLUE);
p6.setBackground(Color.YELLOW);
BorderLayout
BorderLayout
p1.setBackground(Color.RED);
p2.setBackground(Color.GREEN);
p3.setBackground(Color.ORANGE);
p4.setBackground(Color.CYAN);
p5.setBackground(Color.BLUE);
public BorderTest() {
super();
this.setSize(300, 200);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel mainPanel = new JPanel();
25
10/9/2014
LayoutManagers
LayoutManagers
93
of 95
100/106
Swing: Summary
Announcements
o
o
o
26