该JSlider的setLabelTable()方法可以让你定义了一个自定义标签JSlider。标签表是一个Hashtable包含Integer数字作为键和一个JLabel组件作为其值的表。整数键对应于JSlider将自定义标签的刻度。
package org.nhooo.example.swing; import javax.swing.*; import java.awt.*; import java.util.Hashtable; public class JSliderCustomLabel extends JPanel { public JSliderCustomLabel() { initializeUI(); } private void initializeUI() { setLayout(new BorderLayout()); setPreferredSize(new Dimension(500, 200)); JSlider slider = new JSlider(JSlider.VERTICAL, 0, 40, 0); slider.setMinorTickSpacing(1); slider.setMajorTickSpacing(5); slider.setPaintTicks(true); Hashtable<Integer, JLabel> labels = new Hashtable<>(); labels.put(0, new JLabel("Freezing")); labels.put(15, new JLabel("Cold")); labels.put(25, new JLabel("Warm")); labels.put(35, new JLabel("Hot")); slider.setLabelTable(labels); slider.setPaintLabels(true); add(slider, BorderLayout.CENTER); } public static void showFrame() { JPanel panel = new JSliderCustomLabel(); JFrame frame = new JFrame("JSlider Custom Label"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JSliderCustomLabel.showFrame(); } }); } }
上面的示例的屏幕截图是: