// SliderExample.java
// An example of JSlider with default labels.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class SliderExample extends JPanel {
JLabel label;
public SliderExample() {
super(true);
this.setLayout(new BorderLayout());
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 40, 20);
JSlider slider2 = new JSlider(JSlider.VERTICAL, 0, 40, 20);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider2.setMinorTickSpacing(2);
slider2.setMajorTickSpacing(10);
slider2.setPaintTicks(true);
slider2.setPaintLabels(true);
label=new JLabel();
label.setText("<html><font color=red size=+2>"+"the value is "+slider.getValue());//只用了半边的html!!!也行??
// We'll just use the standard numeric labels for now...
slider.setLabelTable(slider.createStandardLabels(10));//这句重新给slider的MajorTickSpacing 赋值了
//从而调整了slider的主要核对符号的间隔
add(slider, BorderLayout.SOUTH);
add(slider2, BorderLayout.EAST);
add(label, BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Slider Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SliderExample());
// frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
}
}