首页javajsliderJava Swing - 如何在任何特定的主刻度标记处设置任意标签

Java Swing - 如何在任何特定的主刻度标记处设置任意标签

我们想知道如何在任何特定的主刻度标记处设置任意标签。

import java.util.Dictionary;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    
    Dictionary table = slider.getLabelTable();

    ImageIcon icon = new ImageIcon("icon.gif");
    JLabel label = new JLabel(icon);

    // Set at desired positions
    table.put(new Integer(slider.getMinimum()), label);
    table.put(new Integer(slider.getMaximum()), label);

    // Force the slider to use the new labels
    slider.setLabelTable(table);

  }
}