获取图标代码:
//获取slider的图标
UIDefaults defaults = UIManager.getDefaults();
Icon icon = defaults.getIcon("Slider.horizontalThumbIcon" );
测试代码:
public static void main(String[] args) {
JFrame frame = new JFrame("获得图标测试");
Box mainBox = new Box(BoxLayout.Y_AXIS);
Box sliderBox = new Box(BoxLayout.Y_AXIS);
Box labelBox = new Box(BoxLayout.Y_AXIS);
//一个普通的滑动条
JSlider slider = new JSlider();
JLabel lableSlider = new JLabel("滑动条当前值为: " + slider.getValue());
JLabel lable2 = new JLabel("JSlider的图标", JLabel.CENTER);
//监听器
ChangeListener listener = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
lableSlider.setText("滑动条当前值为: " + source.getValue());
}
};
//为slier绑定监听事件
slider.addChangeListener(listener);
//获取slider的图标
UIDefaults defaults = UIManager.getDefaults();
Icon icon = defaults.getIcon("Slider.horizontalThumbIcon" );
//将slider图标设置为label2的图标
lable2.setIcon(icon);
sliderBox.add(slider);
sliderBox.add(lableSlider);
labelBox.add(lable2);
mainBox.add(sliderBox);
mainBox.add(labelBox);
frame.add(mainBox);
frame.setSize(300, 200);
frame.setVisible(true);
}
运行结果: