0
以下是我正在嘗試做的事情 - 長話短說: 構建一個窗口(將稱爲MainWindow),頂部有按鈕,中心有一個圖片。我想給用戶選擇選擇做什麼(按鈕),而每隔幾秒鐘更改中心圖片。給用戶的部分選項是「暫停」和「恢復」 - 控制序列。 基本上嘗試通過線程更新GUI(MainWindow)。此線程將運行,而布爾'playSequence'將爲真。通過java中的線程顯示一系列圖像
有人能解釋我爲什麼不能得到它的工作..
這裏是代碼:
package SpecializedControls;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import DataEntities.SgiImage;
public class SgiImagePanel extends JPanel implements Runnable {
private List<SgiImage> seqImageList;
private JLabel lastImage;
private boolean playSequence;
public SgiImagePanel(){}
public SgiImagePanel (List<SgiImage> sequenceList)
{
seqImageList = sequenceList ;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(playSequence)
{
for (SgiImage image : seqImageList)
{
display(image);
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void display(SgiImage image)
{
reset();
JLabel picLabel = new JLabel(new ImageIcon(image.getImage()));
add(picLabel);
lastImage = picLabel;
}
public void display(List<SgiImage> sequenceList)
{
if(sequenceList==null)
return;
playSequence = true;
SgiImagePanel seq = new SgiImagePanel(sequenceList);
Thread thread = new Thread(seq);
thread.start();
}
public void reset(){
if (lastImage != null)
{
remove(lastImage);
lastImage = null;
}
}
public void pause() {
playSequence = false;
}
public void resume(){
playSequence = true;
}
}
已經有一個線程畫在GUI內容。它被稱爲gui線程。其他人可以顯示的唯一方式是向其發送消息。其他線程不應該觸摸GUI對象。你知道同步,爲什麼使用它? – Val