我正在为模拟创建一个简单的“播放/倒带”界面,其中的 JSlider 既可用作进度条,也可用作 slider ,您可以将模拟移动到模拟范围内的任何时间点。滑动到时间点效果很好,但是当模拟“播放”时,我无法让实际 slider 移动。下面显示了 GUI 的链接以获取更多帮助:
我正在做的事情的简化版本
主要对象有 2 个对象,GUI 和 Controller 。 gui:创建 slider /按钮和 Controller 。 slider 和按钮的更改/操作监听器设置为 Controller
Controller :执行播放/快退功能的各种按钮。这是通过使用 Swing 计时器来处理的,该计时器会在每个“帧”重新绘制模拟。使用 slider ,当您拖动 slider 时,“CurrentFrame”会发生变化,并调用重绘函数。它不做的是在计时器运行时自行滑动。 stateChanged 的代码如下:
public void stateChanged(ChangeEvent z)
{
JSlider source = (JSlider)z.getSource();
//If statement that checks if the state of the slider is done changing when the change listener is evoked
if(!source.getValueIsAdjusting())
{
CurrentFrame = (int)source.getValue();
colladaRoot.setPosition(framePos[CurrentFrame]);
wwd.repaint();
}
// My attempt at checking if the timer is running, and changing the value of the slider
// while it is.
while (timer.isRunning())
source.setValue(CurrentFrame);
}
如果有要求,这也是在 GUI 类中创建的 slider :
JSlider framecontroller = new JSlider(0, LastFrame, CurrentFrame);
framecontroller.addChangeListener(this.controller);
framecontroller.setMajorTickSpacing(200);
framecontroller.setMinorTickSpacing(1);
framecontroller.setPaintTicks(true);
framecontroller.setPaintLabels(true);
最佳答案
您不想从 ChangeEventListener
检查计时器状态,它仅在您移动 slider 时才会触发。您需要将 ActionListener
添加到 Timer
并从那里更新 slider
关于java - JSlider 从外部对象移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905140/