这是我第一次参加这个论坛。我有一个java计时器的问题。我的程序在不同的时间显示图像。这是我的代码
//TIMER
//creo timer per il tempo di visualizzazione di ogni singola immagine
timerDisplay = new Timer(this.timeOfImage * MILLE, this);
timerDisplay.setRepeats(false);
timerDisplay.setActionCommand("blackScreen");
//timer tra le immagini
timerBetweenImages = new Timer(this.timeBetwImage * MILLE,this);
timerBetweenImages.setRepeats(false);
timerBetweenImages.setActionCommand("image");
//timer prima della scelta
timerBeforeChoice = new Timer(this.timeBeforeChoice * MILLE,this);
timerBeforeChoice.setRepeats(false);
timerBeforeChoice.setActionCommand("Choice");
//tempo di scelta
timerChoice = new Timer(this.timeOfChoice * MILLE,this);
timerChoice.setRepeats(false);
timerChoice.setActionCommand("End");
public void actionPerformed(ActionEvent e) {
// tempo tra immagini consecutive
if (e.getActionCommand().equals("blackScreen")) {
label.setIcon(null);
if (index != numOfImages) {
timerBetweenImages.start();
} else {
label.setIcon(null);
timerBeforeChoice.start();
}
//to see the images
} else if (index < numOfImages && e.getActionCommand().equals("image")){
label.setIcon(image[index]);
index++;
//visualizza
timerDisplay.start();
state();
} else if (e.getActionCommand().equals("Choice")) {
contentPane.remove(label);
//modifico il layout per inserire tanti pannelli quanto sono le immagini
contentPane.setLayout(new GridLayout(1, this.numOfImages));
repaint();
validate();
//creo tanti pannelli quante sono le immagini da visualizzare
//creo pannello
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLACK);
panel1.setLayout(new GridLayout(2,1));
//aggiungo immagine e checkbox al pannello
JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setBackground(Color.BLACK);
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setIcon(image[i]);
panel1.add(lblNewLabel_1);
i++;
chckbxNewCheckBox = new JCheckBox("Immagine " + 1);
chckbxNewCheckBox.setBackground(Color.BLACK);
chckbxNewCheckBox.setForeground(Color.WHITE);
chckbxNewCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
chckbxNewCheckBox.setFont(new Font("Tahoma", Font.BOLD, 14));
panel1.add(chckbxNewCheckBox);
contentPane.add(panel1);
//creo pannello
JPanel panel2 = new JPanel();
panel2.setBackground(Color.BLACK);
panel2.setLayout(new GridLayout(2,1));
//aggiungo immagine e checkbox al pannello
JLabel lblNewLabel_2 = new JLabel("");
lblNewLabel_2.setBackground(Color.BLACK);
lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_2.setIcon(image[i]);
panel2.add(lblNewLabel_2);
i++;
chckbxNewCheckBox2 = new JCheckBox("Immagine " + 2);
chckbxNewCheckBox2.setBackground(Color.BLACK);
chckbxNewCheckBox2.setForeground(Color.WHITE);
chckbxNewCheckBox2.setHorizontalAlignment(SwingConstants.CENTER);
chckbxNewCheckBox2.setFont(new Font("Tahoma", Font.BOLD, 14));
panel2.add(chckbxNewCheckBox2);
//i++;
//aggiungo pannello al contentPane
contentPane.add(panel2);
repaint();
validate();
chckbxNewCheckBox2.addItemListener(this);
chckbxNewCheckBox.addItemListener(this);
state();
} else if (e.getActionCommand().equals("End")) {
contentPane.removeAll();
repaint();
validate();
index = this.numOfImages;
//System.out.println(index);
this.numOfImages = this.numOfImages + number;
//System.out.println(numOfImages);
contentPane.setLayout(new GridLayout(1, 1));
contentPane.add(label);
if (numOfImages <= image.length) {
timerDisplay.start();
} else {
//CHIUDI CONNESSIONE
Edk.INSTANCE.EE_EmoStateFree(eState);
Edk.INSTANCE.EE_EmoEngineEventFree(eEvent);
Edk.INSTANCE.EE_EngineDisconnect();
dispose();
}
}
然后我有这个方法:
public void state(){
while(timerDisplay.isRunning()){
state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent);
// New event needs to be handled
if (state == EdkErrorCode.EDK_OK.ToInt()) {
int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent);
Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID);
// Log the EmoState if it has been updated
if (eventType == Edk.EE_Event_t.EE_EmoStateUpdated.ToInt()) {
Edk.INSTANCE.EE_EmoEngineEventGetEmoState(eEvent, eState);
//tempo
float timestamp = EmoState.INSTANCE.ES_GetTimeFromStart(eState);
//user ID
int user = userID.getValue();
//valori emotional
float shortExcitement = EmoState.INSTANCE.ES_AffectivGetExcitementShortTermScore(eState);
float longExcitement = EmoState.INSTANCE.ES_AffectivGetExcitementLongTermScore(eState);
float egagement = EmoState.INSTANCE.ES_AffectivGetEngagementBoredomScore(eState);
float frustation = EmoState.INSTANCE.ES_AffectivGetFrustrationScore(eState);
float meditation = EmoState.INSTANCE.ES_AffectivGetMeditationScore(eState);
if(option == 1){
output.println(timestamp + ";" + user + ";" + shortExcitement + ";" +
longExcitement + ";" + egagement + ";" + frustation + ";" + meditation + ";");
} else if (option == 2){
output.println(timestamp + ";" + user + ";" + shortExcitement + ";" +
longExcitement + ";" + egagement + ";" + frustation + ";" + meditation + ";");
}
}
}
else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) {
output.println("Internal error in Emotiv Engine!");
//messaggio di errore
String message2 = "Internal error in Emotiv Engine!";
JOptionPane.showMessageDialog(new JFrame(), message2, "Dialog",
JOptionPane.ERROR_MESSAGE);
}
}
}
问题在于我的state()
方法,当我调用timerDisplay.isRunning()
时,程序无法显示图像。实际上,我希望state()
一直运行直到timerDisplay
完成。对不起我的英语不好......:)
答案 0 :(得分:2)
您的state
方法似乎阻止了事件调度线程。这可以防止处理任何事件(包括绘制和计时器事件),使其看起来像应用程序已停止。
这是因为Swing是单线程环境。这意味着必须在Event Dispatching Thread的上下文中执行与UI的所有交互。这也意味着您应该在另一个线程上执行所有阻塞或耗时的处理。
看看SwingWorker
。这将允许您在后台线程中处理“状态”事件,但也有易于使用的方法来重新同步UI / EDT的任何更新。
还有阅读Concurrency in Swing以获取更多信息