我正在尝试模仿(或找到一个预先存在的组件)来模仿 Word 2007 中的缩放滑块:
这个组件和标准的 Java JSlider 有两个主要区别:
- 除了 100% 时不会捕捉到刻度,并且在您滑动条时而不是在释放鼠标时捕捉
- 滑块在整个过程中不是线性的:滑块的左半部分从 10% 变为 100%;右侧从 100% 变为 500%。
来源:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author NDUNN
* @date Nov 25, 2009
*/
public class ZoomBar extends JPanel implements ActionListener, ChangeListener {
private JLabel zoomAmount;
private JButton minus;
private JButton plus;
private JSlider slider;
private static final int MIN_ZOOM = 10;
private static final int MAX_ZOOM = 200;
private static final int DEFAULT_ZOOM = 100;
private static final int MAJOR_ZOOM_SPACING = 50;
private static final int MINOR_ZOOM_SPACING = 10;
public ZoomBar() {
super();
minus = new JButton("-");
plus = new JButton("+");
slider = new JSlider(MIN_ZOOM, MAX_ZOOM, DEFAULT_ZOOM);
slider.setMinorTickSpacing(MINOR_ZOOM_SPACING);
slider.setMajorTickSpacing(MAJOR_ZOOM_SPACING);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
zoomAmount = new JLabel(slider.getValue() + "%");
add(zoomAmount);
add(minus);
add(slider);
add(plus);
plus.addActionListener(this);
minus.addActionListener(this);
slider.addChangeListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Zoom bar clone");
frame.setContentPane(new ZoomBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == plus) {
slider.setValue(slider.getValue() + MINOR_ZOOM_SPACING);
}
else if (e.getSource() == minus) {
slider.setValue(slider.getValue() - MINOR_ZOOM_SPACING);
}
}
public void stateChanged(ChangeEvent e) {
if (slider.getValueIsAdjusting()) {
return;
}
zoomAmount.setText(slider.getValue() + "%");
}
}
基本上只是模仿外观,但没有上面提到的这两个功能。我在JSlider API中看不到任何可以让我获得这种行为的东西。我是否必须从头开始才能获得这种行为?如果是这样,那不值得我花时间,但是如果有人知道获得这种行为的好方法,我认为在我正在从事的项目中拥有它会很好。
谢谢,
缺口
编辑:
感谢 PSpeed 关于将 0..50 和 50..100 映射到不同值的想法。执行此操作的代码如下。
不幸的是,将 setValue 更改为 snap 的想法没有奏效。
public void stateChanged(ChangeEvent e) {
// While slider is moving, snap it to midpoint
int value = slider.getValue();
if (slider.getValueIsAdjusting()) {
return;
}
zoomValue = fromSlider(value);
zoomLabel.setText(zoomValue + "%");
}
public int fromSlider(int sliderValue) {
int mappedValue = 0;
if (sliderValue <= 50) {
// Map from [0, 50] to [MIN ... DEFAULT]
mappedValue = (int) map(sliderValue, 0, 50, MIN_ZOOM, DEFAULT_ZOOM);
}
else {
// Convert from (50, 100] to (DEFAULT ... MAX]
mappedValue = (int) map(sliderValue, 50, 100, DEFAULT_ZOOM, MAX_ZOOM);
}
return mappedValue;
}
public int toSlider(int modelValue) {
int mappedValue = 0;
if (modelValue <= DEFAULT_ZOOM) {
// Map from [MIN_ZOOM, DEFAULT_ZOOM] to [0 ... 50]
mappedValue = (int) map(modelValue, MIN_ZOOM, DEFAULT_ZOOM, 0, 50);
}
else {
// Convert from (DEFAULT ... MAX] to (50, 100]
mappedValue = (int) map(modelValue, DEFAULT_ZOOM, MAX_ZOOM, 50, 100);
}
return mappedValue;
}
/**
* @param value The incoming value to be converted
* @param low1 Lower bound of the value's current range
* @param high1 Upper bound of the value's current range
* @param low2 Lower bound of the value's target range
* @param high2 Upper bound of the value's target range
* @return
*/
public static final double map(double value, double low1, double high1, double low2, double high2) {
double diff = value - low1;
double proportion = diff / (high1 - low1);
return lerp(low2, high2, proportion);
}
public static final double lerp(double value1, double value2, double amt) {
return ((value2 - value1) * amt) + value1;
}
编辑 2:我注意到的另一个区别是 JButton 不允许您按住它多次触发按钮,而办公室中的 +/- 按钮可以。知道如何模仿吗?