Java 我如何才能使jslider在滑动时真正影响事物?
我在这里设置了一个区域,jslider应该在这里改变jpanel中一些点的延迟Java 我如何才能使jslider在滑动时真正影响事物?,java,swing,jpanel,jslider,Java,Swing,Jpanel,Jslider,我在这里设置了一个区域,jslider应该在这里改变jpanel中一些点的延迟 JSlider source = (JSlider)e.getSource(); if (source == speedSlider) { if (source.getValueIsAdjusting()) { GraphicsDisplay.delay += 100000; }
JSlider source = (JSlider)e.getSource();
if (source == speedSlider) {
if (source.getValueIsAdjusting()) {
GraphicsDisplay.delay += 100000;
}
}
延迟由以下因素影响:
public static boolean stop = true ;
public static long delay = 3000000 ;
public void paint ( Graphics g2 ) {
//code making dots up here...
int a;
if ( !stop ) {
for ( a=0; a<delay; a++ ) ;
moveDot ( ) ;
}
repaint();
}
问题不在于滑块,而在于你的绘画
public void paint ( Graphics g2 ) {
// Let's ignore the fact that you haven't called super.paint here...
//code making dots up here...
int a;
if ( !stop ) {
// Block the EDT, stop all painting and event processing until this for
// exist...
for ( a=0; a<delay; a++ ) ;
moveDot ( ) ;
}
// This is a very bad idea inside any paint method...
repaint();
}
基本上,您正在阻止事件调度线程,阻止它实际绘制
public void paint ( Graphics g2 ) {
// Let's ignore the fact that you haven't called super.paint here...
//code making dots up here...
int a;
if ( !stop ) {
// Block the EDT, stop all painting and event processing until this for
// exist...
for ( a=0; a<delay; a++ ) ;
moveDot ( ) ;
}
// This is a very bad idea inside any paint method...
repaint();
}
您对静态变量的使用也有点吓人
ps-我忘了提到,您应该避免覆盖paint
,而是使用paintComponent
,确保先调用super.paintComponent
,请参阅
private javax.swing.Timer timer;
//...
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
moveDot();
repaint();
}
});
timer.start();
//...
if (source.getValueIsAdjusting()) {
timer.stop();
timer.setDelay(source.getValue());
timer.start();
}