我创建了循环,定期重新绘制组件:
public class A extends Thread {
private Component component;
private float scaleFactors[];
private RescaleOp op;
public A (Component component){
this.component = component;
}
public void run(){
float i = 0.05f;
while (true) {
scaleFactors = new float[]{1f, 1f, 1f, i};
op = new RescaleOp(scaleFactors, offsets, null);
try {
Thread.sleep(timeout);
} catch (InterruptedException ex) {
//Logger.getLogger(...)
}
component.repaint();
i += step;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我收到消息(NetBeans 7.3.1):
Thread.sleep在循环中调用
也许在这种情况下有更好的解决方案?
Swing是单线程的.调用Thread.sleep在EDT阻止UI更新.
我建议改用Swing Timer.它旨在与Swing组件进行交互.
Timer timer = new Timer(timeout, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
component.repaint();
}
});
timer.start();
Run Code Online (Sandbox Code Playgroud)
编辑:
ActionListener通常使用在其自身内停止计时器
@Override
public void actionPerformed(ActionEvent e) {
Timer timer = (Timer) e.getSource();
timer.stop();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1882 次 |
| 最近记录: |