This lab exercise will familiarize you with creating Swing GUI
components and giving them behavior through listeners.
Here is a zipped NetBeans project to get you started:
CS_2511_Listeners.zip.
Unzip the file and open the resulting
CS_2511_Listeners project
in NetBeans.
When you complete this exercise, you will have an application that
behaves like that below.
Try clicking the button and moving the slider knob:
Your job is to match the behavior and appearance of these components.
You will complete the
listeners.Listeners class shown below.
This section lists the steps you should take to accomplish this.
Verify that you've correctly opened the NetBeans project by running
the
Listeners.java file.
You should get an empty frame that looks like:
Add a slider to the frame so that running
Listeners.java
produces:
- Use the JSlider constructor with no arguments to obtain
a slider whose value ranges from 0 to 100.
Add a label to the frame so that running
Listeners.java
produces:
- Use JLabel's setText method to set the label's
text to the value of the slider:
- Use JSlider's getValue method to get the slider's
initial value, which is 50
- Use the setPreferredSize method that JLabel
inherits from JComponent to make the label 60 pixels wide
and 50 pixels high
- Use the setFont method that JLabel inherits from
JComponent to render the button's text in a bold, sans
serif type with a size of 20 points
- Use the setForeground method that JLabel inherits
from JComponent to give the label a blue foreground color
- Use the setBorder method that JLabel inherits
from JComponent to give the label a titled border:
- Use the javax.swing.border.TitledBorder class to create a
titled border
Add a change listener to the slider so that when it is moved, the label
displays the slider's value:
- See the javax.swing.event.ChangeListener interface
- The listener can set the text of the label the same way it was
set upon construction
- Use JSlider's addChangeListener method to add the
listener
When finished, zip your project folder as
your-login-LEX5.zip.
Email the zip file to your TA.
- Correct button appearance and behavior: 4 points
- Correct slider and label appearance and behavior: 6 points