我试图为JSlider
制作一个非线性比例,但是,我目前使用的比例差距很大,这使得它不太理想。
这是因为我设置分区的方式。例如,当滑块位于300
时,精度为1661
,但当滑块位于301
时,精度为9192
。
是否有办法在不增加额外范围的情况下缩小间隙。例如,没有做很多
else if(temp > 150 && temp <= 250)
accuracy = (int) Math.pow((double) accSlide.getValue(), 1.2);
修改
这个JSlider
的目的是在数字之间有3到4个不同的加速部分。例如
Section 1 (First quarter of JSlider) : 0-150
Section 2 (Second quarter of JSlider): 150-10000
Section 3 (Third quarter of JSlider) : 10000-100000
Section 4 (Final quarter of JSlider) : 100000-1000000
稍后将在程序的不同函数中使用此accuracy
变量。在以下简短代码示例中,它显示为更新JLabel
。使用JSlider
SSCCE / MCVE
import java.awt.EventQueue;
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;
@SuppressWarnings("serial")
public class Slider extends JFrame {
private int accuracy = 150;
private Slider() {
super("Slider");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createLayout();
setSize(400, 100);
setLocationRelativeTo(null);
setVisible(true);
}
private void createLayout() {
JPanel rootPanel = new JPanel();
JLabel acc = new JLabel("Accuracy: " + accuracy);
JSlider accSlide = new JSlider(1, 1000, accuracy);
accSlide.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
int temp = accSlide.getValue();
if(temp <= 150)
accuracy = accSlide.getValue();
else if(temp > 150 && temp <= 300)
accuracy = (int) Math.pow((double) accSlide.getValue(), 1.3);
else if(temp > 300 && temp <= 600)
accuracy = (int) Math.pow((double) accSlide.getValue(), 1.6);
else if(temp > 600 && temp <= 1000)
accuracy = (int) Math.pow((double) accSlide.getValue(), 2);
acc.setText("Accuracy: " + accuracy);
}
});
rootPanel.add(acc);
rootPanel.add(accSlide, "grow");
add(rootPanel);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new Slider());
}
}
答案 0 :(得分:1)
有一些简单的方法可以做到这一点,但重要的是从前一个范围的最大值开始; e.g。
public void stateChanged(ChangeEvent arg0) {
int temp = accSlide.getValue();
double max=0;
if(temp <= 150)
accuracy = accSlide.getValue();
else if(temp <= 300)
accuracy = (int) (150+2*(accSlide.getValue()-150));
else if(temp <= 600)
accuracy = (int) (150+2*(300-150)+3*(accSlide.getValue()-300));
else
accuracy = (int) ( (150+2*(300-150)+3*(600-300))+4*(accSlide.getValue()-600));
acc.setText("i " + temp+" Accuracy: " + accuracy);
}
});
或
public void stateChanged(ChangeEvent arg0) {
int temp = accSlide.getValue();
double max=0;
if(temp <= 150)
accuracy = accSlide.getValue();
else if(temp <= 300) {
max=150;
accuracy = (int) (max+2*(accSlide.getValue()-150));
}
else if(temp <= 600) {
max=150+2*(300-150);
accuracy = (int) (max+3*(accSlide.getValue()-300));
}
else {
max=150+2*(300-150)+3*(600-300);
accuracy = (int) ( (max+4*(accSlide.getValue()-600));
acc.setText("i " + temp+" Accuracy: " + accuracy);
}
});
对于一系列斜率较高的线。
或者你的例子:
if(temp <= 150)
accuracy = accSlide.getValue();
else if(temp <= 300)
accuracy = (int) (150+Math.pow(accSlide.getValue()-150, 1.3));
else if(temp <= 600)
accuracy = (int) (150+Math.pow(300-150, 1.3)+Math.pow((double) accSlide.getValue()-300, 1.6));
else
accuracy = (int) ( (150+Math.pow(300-150, 1.3)+Math.pow(600-300, 1.6))+Math.pow((double) accSlide.getValue()-600, 2));
acc.setText("i " + temp+" Accuracy: " + accuracy);