这是我的代码的一部分,它不起作用,因为它一直说它无法在我的ActionListener
中找到符号。我不知道如何让它发挥作用。
所以基本上我要做的是让1-8.png的图像移动取决于滑块落在何处以及IDK如何:
private static JLabel value;
private static ImageIcon image;
private static Timer timer;
private static final int delay = 2000;
private static int newDelay;
private static int i = 1;
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// makes the image at i appear and then goes to 2 and so on until i i = 8 and will return a 1 after. Will keep on doing so
value.setIcon(new ImageIcon(i + ".png"));
i++;
if(i == 8) {
i = 1;
}
}
});
timer.start();
}
private static class SliderChange implements ChangeListener {
public void stateChanged(ChangeEvent event) {
JSlider source = (JSlider) event.getSource();
// while it is adjusting timer stops and gets the value of where the slider hits and the newDelay will be the new timer time. (So if they drag slider to 6, delay(which is 2000) will be divided by 6 to get new time
if (!source.getValueIsAdjusting()) {
timer.stop();
value.setIcon(new ImageIcon(i + ".png"));
newDelay = (delay/(int)source.getValue());
timer = new Timer(newDelay, new Actionlistener());
timer.start();
}
}
}
但这不起作用。我该如何解决?
它指向此行说错误:
timer = new Timer(newDelay, new Actionlistener());
答案 0 :(得分:1)
我不确定每次重新创建Timer
是否真的有必要。相反,停止它,设置它的delay
属性并重新启动它
private static class SliderChange implements ChangeListener {
public void stateChanged(ChangeEvent event) {
JSlider source = (JSlider) event.getSource();
// while it is adjusting timer stops and gets the value of where the slider hits and the newDelay will be the new timer time. (So if they drag slider to 6, delay(which is 2000) will be divided by 6 to get new time
if (!source.getValueIsAdjusting()) {
timer.stop();
value.setIcon(new ImageIcon(i + ".png"));
newDelay = (delay/(int)source.getValue());
timer.setDelay(newDelay);
timer.start();
}
}
}
问题是timer = new Timer(newDelay, new Actionlistener());
正在尝试创建interface
的实例而没有实现interface
的要求,这会让编译器感到困惑