2014-01-08 69 views
0

我在JFrame上加JLabel並在YES按鈕上點擊顯示框JOptionPane,它顯示框架但沒有顯示標籤文字。JLabel文字未顯示

enter image description here

int yes = JOptionPane.showConfirmDialog(null,"Do you want to reactivate previous 
     schedule(s)","Reactivate Schedule",JOptionPane.OK_CANCEL_OPTION, 
     JOptionPane.INFORMATION_MESSAGE); 

    if(yes == JOptionPane.OK_OPTION) { 
     setVisible(false); 
     disp_wait.setVisible(true); 
     for(int i=0 ; i<options.taskList.size(); i++) { 
      dataList = Options.getInstance().getTaskList(); 
      Task task=dataList.get(i); 
      boolean active = task.getActive(); 
      if(active) { 
       task.setActive(true); 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException ex) { 
        ex.getMessage(); 
       } 
      } 
     } 
    } 

enter image description here

+2

不要阻塞EDT(事件調度線程) - GUI會「凍結」發生。而不是調用'Thread.sleep(n)'實現一個Swing'Timer'來重複執行任務,或者一個'SwingWorker'執行長時間運行的任務。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –

回答

1

所有代碼都在事件處理過程中進行一些處理。在Java中,這是一個問題,只有在處理完所有事件處理後纔會繪製GUI。除此之外,看到你的JFrame的代碼會很高興,它可能不會在調用pack()之前添加標籤()

+0

'JFrame disp_wait; JLabel wait_lbl; disp_wait = new JFrame(); wait_lbl = new JLabel(); disp_wait.setLayout(new FlowLayout()); disp_wait.setSize(300,300); disp_wait.setLocation(200,200); wait_lbl.setText(「Please wait ...」); disp_wait.add(wait_lbl); disp_wait.pack();' – astack