Java Reference
In-Depth Information
RGB mode
The user picks Red, Green, and Blue components by sliders.
Example 14-9
contains a short program that makes it happen.
Example 14-9. JColorDemo.java
public
public class
extends
JFrame
{
/** A canvas to display the color in. */
protected
class
JColorChooserDemo
JColorChooserDemo
extends
protected
JLabel demo
;
/** Constructor - set up the entire GUI for this program */
public
public
JColorChooserDemo
() {
super
super
(
"Swing Color Demo"
);
Container cp
=
getContentPane
();
JButton jButton
;
cp
.
add
(
jButton
=
new
new
JButton
(
"Change Color..."
),
BorderLayout
.
NORTH
);
jButton
.
setToolTipText
(
"Click here to see the Color Chooser"
);
jButton
.
addActionListener
(
new
new
ActionListener
() {
public
public
void
void
actionPerformed
(
ActionEvent actionEvent
)
{
Color ch
=
JColorChooser
.
showDialog
(
JColorChooserDemo
.
this
,
// parent
"Swing Demo Color Popup"
,
// title
demo
.
getForeground
());
// default
System
.
out
.
println
(
"Your selected color is "
+
ch
);
iif
(
ch
!=
null
null
) {
demo
.
setForeground
(
ch
);
demo
.
repaint
();
}
}
});
cp
.
add
(
BorderLayout
.
CENTER
,
demo
=
new
new
JLabel
(
"Your One True Color"
,
JLabel
.
CENTER
));
demo
.
setToolTipText
(
"This is the last color you chose"
);
pack
();
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
}
/** good old main */
public
public static
static
void
void
main
(
String
[]
argv
) {
new
new
JColorChooserDemo
().
setVisible
(
true
true
);
}
}