滑动条
组合框在离散值中选择,滚动条从连续值中选择。
构造滑动条最常用方法:var slider = new JSlider(min,max,initaValue);
如果忽略参数,最大值,最小值,初始值分别为0,100,50.
垂直滑动条,new JSlide(SwingConstants.VERTICAL,min,max,initValue);
滑动时,addChangeListener安装了一个ChangeListener接口对象。
ChangetListener listener = event -> {
JSlider slider = (JSlider)event.getSource();
int value = slider.getValue();
...
}
可以通过刻度(tick)对滑块进行修饰。
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
每20个单位显示一个大刻度,每5个单位显示一个小刻度。
显示的话,slider.setPaintTicks(true);
大小刻度独立,强制对齐刻度(snap to tick),silder.setSnapToTicks(true)
为大刻度标记标签(tick mark label),slider.setPaintLabels(true)。
滑动条不同视觉效果
package swing;import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.util.Hashtable;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;public class SliderTest {private static JPanel sliderPanel = new JPanel();public static void main(String[] args) {JFrame frame = new JFrame("滑动条");JTextField field = new JTextField();sliderPanel.setLayout(new GridBagLayout());ChangeListener listener = e -> {JSlider source = (JSlider) e.getSource();field.setText(""+source.getValue());};var slider = new JSlider();addSilder(listener,slider,"Plain");slider = new JSlider();slider.setPaintTicks(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);addSilder(listener, slider, "Ticks");slider = new JSlider();slider.setPaintTicks(true);slider.setSnapToTicks(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);addSilder(listener, slider, "Snap to ticks");slider = new JSlider();slider.setPaintTicks(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);slider.setPaintTrack(false);addSilder(listener, slider, "No track");slider = new JSlider();slider.setPaintTicks(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);slider.setInverted(true);addSilder(listener, slider, "Inverted");slider = new JSlider();slider.setPaintTicks(true);slider.setPaintLabels(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);addSilder(listener, slider, "Labels");slider = new JSlider();slider.setPaintTicks(true);slider.setPaintLabels(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(5);var labelTable = new Hashtable<Integer, Component>();labelTable.put(0, new JLabel("A"));labelTable.put(20, new JLabel("B"));labelTable.put(40, new JLabel("C"));labelTable.put(60, new JLabel("D"));labelTable.put(80, new JLabel("E"));labelTable.put(100, new JLabel("F"));slider.setLabelTable(labelTable);addSilder(listener, slider, "Custom labels");slider = new JSlider();slider.setPaintTicks(true);slider.setPaintLabels(true);slider.setSnapToTicks(true);slider.setMajorTickSpacing(20);slider.setMinorTickSpacing(20);labelTable = new Hashtable<Integer, Component>();labelTable.put(0, new JLabel(new ImageIcon()));labelTable.put(20, new JLabel(new ImageIcon()));labelTable.put(40, new JLabel(new ImageIcon()));labelTable.put(60, new JLabel(new ImageIcon()));labelTable.put(80, new JLabel(new ImageIcon()));labelTable.put(100, new JLabel(new ImageIcon()));slider.setLabelTable(labelTable);addSilder(listener, slider, "Icon labels");frame.add(sliderPanel,BorderLayout.CENTER);frame.add(field,BorderLayout.SOUTH);frame.setSize(600, 400);//初始打开为屏幕中央Toolkit toolkit = Toolkit.getDefaultToolkit();Dimension screenSize = toolkit.getScreenSize();int screenWidth = (int) screenSize.getWidth();int screenHeight = (int) screenSize.getHeight();//计算窗口位置int x = (screenWidth - frame.getWidth())/2;int y = (screenHeight - frame.getHeight())/2;frame.setLocation(x, y);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}public static void addSilder(ChangeListener listener, JSlider slider,String desc) {slider.addChangeListener(listener);var panel = new JPanel();panel.add(slider);panel.add(new JLabel(desc));panel.setAlignmentX(Component.LEFT_ALIGNMENT);var gbc = new GridBagConstraints();gbc.gridy = sliderPanel.getComponentCount();gbc.anchor = GridBagConstraints.WEST;sliderPanel.add(panel,gbc);} }