Java,无限循环的替代? [英] Java, replacement for infinite loops?
问题描述
我正在制作一个通过阵列显示细胞生长的程序。当我按下开始按钮时,我已经得到它,数组在一段时间(真实){}循环中每10秒更新一次。问题是我希望能够通过按下暂停按钮来停止循环,但是在循环中,它不会让我使用任何控件。我需要在orer中使用除了无限循环之外的其他东西来刷新帧。
I am making a program that shows cellular growth through an array. I have gotten it so when I press the start button, the array updates every 10 seconds in a while(true){} loop. The problem with that is I want to be able to stop the loop by pressing the pause button, but while in the loop, it wont let me use any of the controls. I need something other than a infinite loop in orer to refresh the frames.
我是一个新手,但我目前正在参加java课程。所以我掌握了一些语言。
I am a bit of a newbie, but I am in a java class at the moment. so I have some grasp of the language.
推荐答案
你需要的是使用计时器改变组件的状态(在这种情况下是细胞增长),然后调用 JComponent.repaint()
What you need is use a Timer which changes the state of your component ( in this case the cellular growth ) and then call JComponent.repaint()
此计时器可以是取消进行暂停,然后重新启动它,你只需创建一个新的一个:
This timer can be cancelled to make the pause and then to restart it, you just create a new one:
所以你可以定义以下两种方法:
So you could define the following two methods:
private Timer timer;
...
public void startPaiting() {
timer = new Timer();
timer.schedule( new TimerTask(){
public void run(){
changeState();
repaint();
}
},0, 10000 ); // 10 s.
}
public void pause(){
timer.cancel();
}
然后在暂停/恢复按钮中调用pause / startPaiting 方法:
And then in your "Pause/Resume" button invoke this "pause/startPaiting" methods:
if( e.getActionCommand().equals("Pause")){
growPanel.pause();
setText("Resume");
} else {
growPanel.startPaiting();
setText("Pause");
}
http://img512.imageshack.us/img512/7119/capturadepantalla201001.png
以下是完整的源代码它运行:
Here's the complete source code to see it running:
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
public class Grow {
public static void main( String [] args ) {
JFrame frame = new JFrame();
final GrowPanel growPanel = new GrowPanel();
frame.add( growPanel );
frame.add( new JPanel(){{
add( new JButton("Pause"){{
addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
if( e.getActionCommand().equals("Pause")){
growPanel.pause();
setText("Resume");
} else {
growPanel.startPaiting();
setText("Pause");
}
}
});
}});}}, java.awt.BorderLayout.SOUTH );
frame.setSize( 400, 300 );
frame.setVisible( true );
}
}
class GrowPanel extends JComponent {
private int x;
private int y;
private Timer timer;
GrowPanel() {
x = 10;
y = 10;
startPaiting();
}
public void startPaiting() {
timer = new Timer();
timer.schedule( new TimerTask(){
public void run(){
changeState();
repaint();
}
},0, 100 ); // or 10000 which is 10 s.
}
public void pause(){
timer.cancel();
}
public void paintComponent( Graphics g ){
g.fillOval( x, y, 10, 10 );
}
private void changeState(){
x+=10;
if( x >= 400 ) {
y+=10;
x = 0;
}
if( y >= 300 ){
y = 10;
}
}
}
这篇关于Java,无限循环的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!