Import the Swing border package.
Get a reference to the internal contents area of the JFrame
.
Create a 2 row by 3 column grid to layout the buttons in. Leave a 5 pixel
horizontal and vertical gap, to better show where the various buttons begin
and end.
For the first button, create a JButton
and set its border to a
gray LineBorder
. Have the button's label be "One," and
add it to the display.
For the second button, create a JButton
and set its border to
be a EtchedBorder
. Have the button's label be "Two,"
and add it to the display.
For the third button, create a JButton
and set its border to
be empty, with two pixels of space all around. EmptyBorder
is
the empty border. Have the button's label be "Three," and add
it to the display.
For button four, use a TitledBorder
to display a title of
"Press Me" in a blue, 10-point italic Serif font, centered
on the top of a black LineBorder
. Have the button's label be
"Four," and add it to the display next.
Before creating the fifth button, create a subclass of JButton
that will show a different border when the button is selected (pressed-in).
Call the class DoubleBorderedButton
and have its constructor accept
two Border
objects as parameters. Add a MouseListener
and
have it toggle between the borders when the mouse button is pressed or not.
Now, for the fifth button, create a DoubleBorderedButton
. Have
one of the borders be a raised BevelBorder
with a blue highlight
color and a yellow shadow. For the other, have the border be a lowered
BevelBorder
with the same colors. Have the button's label be
"Five," and add it to the display.
The final button uses a new border, DashedBorder
that shows a
dashed border line, instead of the solid one used with LineBorder
.
Before you can use it though, you need to complete the definition. The
Border
interface has three methods, paintBorder()
,
isBorderOpaque()
, and getBorderInsets()
. The
paintBorder()
and isBorderOpaque()
methods are already
defined. Define the getBorderInsets()
method to return an
Insets
object that is THICKNESS
wide/high all around.
THICKNESS
is a constant defined within the DashedBorder
class.
Since the code to add the sixth button is already present, you only need
to save everything and compile the program. Then run it to see the results.
Be sure to select the fifth and sixth buttons to show the selected border.
As a precaution, the Save command appends a "1"
to the end of the filename you want to save. This can help
prevent you from accidently overwriting your source code.