JButtons
|
|
2. In the object variable section add this JButton b1, b2, b3, b4; 3. Add these in the
create object section 4. Add these in the container.add section container.add(b1); It should look like what you see on the right |
![]() |
5. Each JButton is a different size. By twicking what is inside the quotes, we can make each JButton almost the same size. Make the following changes
| Change this | to this |
It will look like
this |
|
"ONE" |
"--ONE--" |
![]() |
| "TWO" | "--TWO--" | |
| "3" | "-THREE-" | |
| "FOUR" | "-FOUR--" |
6. This is an optional
command - it removes the purple line around a button - compile
and run - see the
difference?
b1.setFocusPainted(false);
7. JButtons are colored the same way as you
color JTextfields - color all 4 buttons differently
b1.setForeground(Color.blue);
b1.setBackground(Color.yellow);
8. ToolTip works on JButtons - add this line
and a tooltip to JButton 2 - verify that they
work
b1.setToolTipText("Click here for
help");
9. Save all
these icons to your unit 3 folder. (you should have these already)
|
We can add one icon to
a button - put a different icon on each button |
![]() |
|
10. Add a container background color - I used darkGray - reminder: this command goes with the container stuff container.setBackground(Color.darkGray); 11. The text and the
icon can be arranged - Use the same commands that we used on
JLabels. My second and third button have horizontal text
alignment - same command that you used for JLabels Make the first and
fourth button have CENTER alignment - one on top and one on the
bottom Notice that the JButtons change size. |
![]() |
12. There are
optional borders for the JButton. - Put
each of these with the correct JButtons.
I used a different one for each button - notice
how they change the size of the buttons
|
b1.setBorder(BorderFactory.createLineBorder(Color.red)); b2.setBorder(BorderFactory.createEmptyBorder( )); b3.setBorder(BorderFactory.createRaisedBevelBorder( )); b4.setBorder(BorderFactory.createLoweredBevelBorder( )); They shifted because adding edging changes the size of the buttons. Normally one would use the same shape on every button. I personally like the edging on b1 or use no border command. |
![]() |
13. This is
very cool. The JButton can have a tooltip and a
RollOver Icon. This icon will appear on where the
current icon is when the mouse is moved over the Button.
b1.setRolloverIcon(new
ImageIcon("icecream.gif"));
Add a rollOver icon to
another JButton
2
JButtons never exist alone. We will now use them with JLabels and JTextfields in the second lab of the assignment.
ASSIGNMENT
|
Lab3c2 - 50 points Start with Lab3 or Lab3c1 - use save as before you start 1. Make
the window size is 200 by 100
2. Use 2
JButton with black background, yellow or pink or cyan
lettering, 3. Position the words centered, below the icon 4. For JButton border use "RaisedBevel" or "LowerBevel" to look like mine. It should change icons as you roll your mouse over the JButtons |
In the picture below, this is what you would see with the icon rollover of the second button.
|
|
Lab3c3 Start with
Lab3 or Lab3c1 - use save as before you
start 1. Make the window size is 200 by 450, change the window name. Use JLabels for the
questions, 2. I used
the no border for the JButtons We will use this one again when we learn to "listen" to the JButtons
|
![]() |