2013-07-24 27 views
1

我有3個分支:Loader,MyDialog和TEST(帶主要方法)。 (代碼見下文)由程序和用戶動作創建的JDialog的不同行爲

一切我想實現的就是創建簡單的對話框與JLabel中的JProgressBar,這將通知用戶多少時間仍然顯示MyDialog。 MyDialog是在構造函數中耗時操作的Jdialog(從數據庫加載數據等)。

在下面的代碼是模型的情況。當通過main創建「MyDialog」(常量BY_USER爲false)時,所有工作正是我想要的。但是當我使用按鈕進行對話時,MyDialog的實例在按鈕按下(常量BY_USER爲true)後創建,Loader是空白的白色窗體。它看起來沒有完成。

加載程序正在擴展線程,所以我想這個問題將在線程(事件調度線程)?我不知道,什麼是錯的,如何修復它。請幫忙。

非常感謝我的英語。

CLASS TEST:

package test; 
import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

public class TEST { 
public static final boolean BY_USER = false; 

public static void main(String[] args) { 
    if (BY_USER) { 
     JFrame mainDialog = new JFrame("Main"); 

     JButton show = new JButton("Show MyDialog"); 
     show.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       MyDialog dialog = new MyDialog(); 
      } 
     }); 
     mainDialog.add(show); 
     mainDialog.setLocationRelativeTo(null); 
     mainDialog.setMinimumSize(new Dimension(160, 80)); 
     mainDialog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     mainDialog.setVisible(true); 
    } else { 
     MyDialog dialog = new MyDialog(); 
    } 
} 
} 

CLASS MyDialog: 封裝測試;

import javax.swing.JFrame; 
import javax.swing.WindowConstants; 


public class MyDialog extends JFrame{ 

public MyDialog() { 
    super(); 

    // making loader with title, first message and count of steps of operation 
    Loader loader = new Loader("Loader", "First showed message", 100); 
    loader.ShowLoader(); 

    // time-consuming operation (loading data from database etc.). 
    // for clarity replaced with for statement 

    int j=0; 
    for(int i=0; i<Integer.MAX_VALUE; i++) 
    { 
     j++;   
     if(j==Integer.MAX_VALUE/100){ 
      // updating loader message and progress bar value 
      loader.NewAction(Integer.MAX_VALUE - i+""); 
      j=0; 
     } 
    } 

    // closing loader 
    loader.DestroyLoader(); 

    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    this.setSize(300, 300); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
} 
} 

類裝載器:

package test; 
import java.awt.BorderLayout; 
import java.awt.Dialog; 
import java.awt.Dimension; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JProgressBar; 
import javax.swing.SwingConstants; 

public class Loader extends Thread{ 
    private JDialog dialog; 
    private JLabel message = new JLabel("", SwingConstants.CENTER); 
    private JProgressBar progressBar = new JProgressBar(0, 100); 
    private String newMessage; 
    private double percentForStep; 
    private int remainingSteps; 

public Loader(String taskName, String firstMessage, int steps) { 
    this.remainingSteps = steps-1; 

    dialog = new JDialog((Dialog) null, taskName); 
    dialog.setLayout(new BorderLayout(15, 15)); 
    dialog.add(message, BorderLayout.CENTER); 
    dialog.add(progressBar, BorderLayout.SOUTH); 
    message.setText(firstMessage);  
    percentForStep = 100/steps;    
} 

public void ShowLoader() 
{ 
    dialog.setMinimumSize(new Dimension(400,120));   
    dialog.setLocationRelativeTo(null); 
    dialog.setVisible(true);   
    this.start(); 
} 

public void DestroyLoader(){   
    dialog.dispose(); 
    this.interrupt(); 
} 

public void NewAction(String newMessage){   
    this.newMessage = newMessage; 
    this.remainingSteps--; 
    Lock.changed = true; 
}  

public int RemainingStepsCount() 
{ 
    return remainingSteps; 
} 

@Override 
@SuppressWarnings({"CallToThreadYield", "SleepWhileInLoop"}) 
public void run() {   
    do{  
     synchronized (Lock.class) { 
      if (Lock.changed) { 
       Lock.changed = false; 
       this.message.setText(newMessage); 
       this.progressBar.setValue((int)(100-(remainingSteps*percentForStep))); 
       dialog.repaint(); 
      } 
      dialog.repaint(); 
     } 
    }while(true); 
} 
} 

class Lock{ 
    static boolean changed = false; 
} 
+1

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –

+1

看到在Swing甲骨文教程併發性,並通過標記問題搜索【JAVA] [擺動] [的JProgressBar] - (http://stackoverflow.com/questions/tagged/java+swing+jprogressbar) – mKorbel

+0

或約閃屏 – mKorbel

回答

0

查找到的SwingWorker和他的使用;我認爲它可以幫助你解決問題。

相關問題