[英]Why is my Thread not cycling?
我有一個問題,我的線程無法循環運行,我不確定為什么。 它只運行一次run方法,然后停止。 這是我的代碼。
public class LifeFrame extends JApplet{
private Scanner scan;
public FramePanel framePanel = new FramePanel();
private JPanel buttonPanel = new JPanel();
private JButton button1 = new JButton("Start");
private JButton button2 = new JButton("Stop");
private JButton button3 = new JButton("Reset");
public ImageIcon onTemp = new ImageIcon(this.getClass().getResource("squareOn.png"));
public ImageIcon offTemp = new ImageIcon(this.getClass().getResource("squareOff.png"));
public Image lifeOn = onTemp.getImage();
public Image lifeOff = offTemp.getImage();
public boolean[][] lifeCheck = new boolean[20][20];
public boolean[][] lifeHolder = new boolean[20][20];
public boolean run = false;
private Thread animator;
public void init(){
try {
scan = new Scanner(new File("life100.txt"));
} catch (FileNotFoundException e) {
System.out.println("ERROR");
}
int temp = Integer.parseInt(scan.nextLine().trim());
for(int i = 0; i < temp; i++)
lifeHolder[Integer.parseInt(scan.next().trim())-1][Integer.parseInt(scan.nextLine().trim())-1] = true;
framePanel.setSize(700,800);
button1.setSize(100,60);
button2.setSize(100,60);
button3.setSize(100,60);
ButtonHandler handler = new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
buttonPanel.setSize(300,60);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.setBackground(new Color(71,68,68));
add(framePanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
startThread();
framePanel.repaint();
}
public void startThread(){
animator = new Thread(framePanel);
animator.start();
}
public class FramePanel extends JPanel implements Runnable{
public FramePanel(){
this.setLayout(new GridLayout(20, 20));
}
public void run(){
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while(run){
cycle();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 500 - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("Sleep Interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
public void cycle(){
int neighbors = 0;
for(int row = 0; row < lifeHolder.length; row++)
for( int col = 0; col < lifeHolder[row].length; col++)
lifeCheck[row][col] = lifeHolder[row][col];
for(int row = 0; row < lifeHolder.length; row++){
for( int col = 0; col < lifeHolder[row].length; col++){
for(int r = row - 1; r <= row + 1; r++){
for(int c = col - 1; c <= col + 1; c++){
if(r >= 0 && c >= 0 && r < lifeHolder.length && c < lifeCheck[row].length)
if(lifeHolder[r][c])
neighbors++;
}
}
if(lifeHolder[row][col]){
neighbors--;
if(neighbors < 2 || neighbors > 3)
lifeCheck[row][col] = false;
}else{
if(neighbors == 3)
lifeCheck[row][col] = true;
}
neighbors = 0;
}
}
for(int row = 0; row < lifeHolder.length; row++)
for( int col = 0; col < lifeHolder[row].length; col++)
lifeHolder[row][col] = lifeCheck[row][col];
repaint();
}
public void paint(Graphics g){
for(int r = 0; r < 20; r++)
for(int r1 = 0; r1 < 20; r1++){
if(lifeHolder[r][r1]){
g.drawImage(lifeOn, r1*20 + 3, r*20 + 29, 20, 20, framePanel);
}else
g.drawImage(lifeOff, r1*20 + 3, r*20 + 29, 20, 20, framePanel);
}
}
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource().equals(button1)){
run = true;
System.out.println(run);
}
if(event.getSource().equals(button2)){
run = false;
System.out.println(run);
}else{
//reset
}
}
}
}
任何幫助,將不勝感激。 謝謝。
您將run
定義為false
。
您啟動線程,線程開始執行run
方法,並且(由於變量run
為false
)結束。
稍后,也許用戶可以單擊按鈕並將變量run
設置為true
。 但是,當用戶可以單擊某些內容時,您的線程已退出很久了。
使用無限循環,使用run
來檢查必須執行的操作或是否必須休眠。 在守護程序上設置線程,以便JVM在最后一個非守護程序線程結束時將其殺死。
因為在啟動時變量run
被定義為false
啟動線程時, run
變量為false。 線程一次通過run
方法,使循環條件失敗,然后退出該方法的底部並終止。
當ButtonHandler
將run
設置為true時,該線程已經死掉了。
建議您不要使用SwingWorker
,而應該嘗試使用自己的線程,而SwingWorker
專門設計用來讓您從GUI事件觸發后台線程中的工作,而不是嘗試管理自己的線程。
剛開始設定
public boolean run = false;
然后做
while(run){
[...]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.