Java Reference
In-Depth Information
setMinorTickSpacing(
int
)
—This method separates minor tick marks by the
specified distance. Minor ticks are displayed as half the height of major ticks.
n
setPaintTicks(
boolean
)
—This method determines whether the tick marks
should be displayed (a
true
argument) or not (a
false
argument).
n
setPaintLabels(
boolean
)
—This method determines whether the numeric label of
the slider should be displayed (
true
) or not (
false
).
n
These methods should be called on the slider before it is added to a container.
Listing 10.2 contains the
Slider.java
source code; the application was shown in Figure
10.11.
LISTING 10.2
The Full Text of
Slider.java
1: import java.awt.event.*;
2: import javax.swing.*;
3:
4: public class Slider extends JFrame {
5:
6: public Slider() {
7: super(“Slider”);
8: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9: JSlider pickNum = new JSlider(JSlider.HORIZONTAL, 0, 30, 5);
10: pickNum.setMajorTickSpacing(10);
11: pickNum.setMinorTickSpacing(1);
12: pickNum.setPaintTicks(true);
13: pickNum.setPaintLabels(true);
14: add(pickNum);
15: }
16:
17: public static void main(String[] args) {
18: Slider frame = new Slider();
19: frame.pack();
20: frame.setVisible(true);
21: }
22: }
Lines 9-14 contain the code that's used to create a
JSlider
component, set up its tick
marks to be displayed, and add the component to a container. The rest of the program is
a basic framework for an application that consists of a main
JFrame
container with no
menus.
In lines 18-20, a new
Slider
object is created, a call to the object's
pack()
method sets
its size to the preferred size of its components, and the object is made visible.