2014-01-23 58 views
-3

我搜索了一路,但無法找到解決方案。 我想更改進度欄中的默認java圖標 ,因爲我使用的是JProgressBar如何更改JProgressBar中的默認Java圖標?

這裏是我的小片段

public class SwingWorkerProgressMonitor implements PropertyChangeListener { 

private static final int PROGRESS_BAR_WIDTH = 200; 
private ProgressMonitor progressMonitor; 
private final SwingWorker<?, ?> task; 
private SwingWorker<Void, Void> backingTask; 
private String message; 
private final JProgressBar progressBar = new JProgressBar(); 
int progress = 0; 
private final String title; 

/* 
* Progress monitor that takes a swing worker and shows its progress 
*/ 
public SwingWorkerProgressMonitor(SwingWorker<?, ?> sw, String title) { 

this.task = sw; 
this.title = title; 

} 

/** 
* This Fu2nction Invoked when task's progress property changes. 
*/ 
@Override 
public void propertyChange(PropertyChangeEvent evt) { 
if (evt.getPropertyName().equals("progress")) { 
    int progress = (Integer) evt.getNewValue(); 
    updateProgress(progress); 
} 

} 

private void updateProgress(int progress) { 
progressMonitor.setProgress(progress); 
message = String.format(PlasaApplication.getTranslator().getString(PlasaLanguageKey.PROGRESS_LODING_DATA) 
    + "(%d%%)", progress); 
progressMonitor.setNote(message); 
if (progressMonitor.isCanceled() || task.isDone()) { 
    Toolkit.getDefaultToolkit().beep(); 
    if (progressMonitor.isCanceled()) { 
    task.cancel(true); 
    } 
} 
} 

/** 
* This function start the progress When click the save button on 
* 
* 
* @param component 
*/ 
public void start(Component component) { 
// creates progress bar 
Dimension preferredSize = progressBar.getPreferredSize(); 
preferredSize.width = PROGRESS_BAR_WIDTH; 
progressBar.setPreferredSize(preferredSize); 
UIManager.put("ProgressMonitor.progressText", title); 
UIManager.put("OptionPane.cancelButtonText", 
    PlasaApplication.getTranslator().getString(PlasaLanguageKey.GLOBAL_CANCEL)); 
progressMonitor = new ProgressMonitor(progressBar, "", "", 0, 100); 
progressMonitor.setProgress(0); 

if (task == null) { 
    throw new IllegalArgumentException("swing worker is not set"); 
} 

backingTask = new BackingTask(task); 
task.addPropertyChangeListener(this); 
backingTask.execute(); 
} 

/** 
* Inner class Here begin the progress status 
* 
*/ 
private class BackingTask extends SwingWorker<Void, Void> { 

SwingWorker<?, ?> worker; // backing worker 

public BackingTask(SwingWorker<?, ?> worker) { 

    this.worker = worker; 

} 

// run the task on background 
@Override 
protected Void doInBackground() throws Exception { 

    worker.execute(); 
    return null; 
}; 

/** 
* This function close the progress monitor when task is done or cancel 
*/ 
@Override 
public void done() { 
    Toolkit.getDefaultToolkit().beep(); 
    progressMonitor.close(); 
} 

} 

}

+3

JProgressBar在哪裏?你在說什麼圖標?你能否提供一個小例子來證明你在做什麼? –

+0

Stackoverflow不是教程網站,它更多的是面向錯誤的幫助程序。您必須向我們提供解決問題的方法,我們很樂意幫助您解決問題。我們不會說「這裏,做這個」,我們會說'而不是這條線,試試這個'或者'你是否考慮過嘗試zzz而不是yyy'?因此,請儘可能向我們提供代碼和堆棧文件 – diazazar

+0

DYM ['ProgressMonitor'](http://docs.oracle.com/javase/7/docs/api/javax/swing/ProgressMonitor.html)? 'JProgressBar'沒有圖標。爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)。 –

回答

1

因此,假設你想改變你的應用程序(JFrame)的圖標,你想要做一個進步的子類,嘗試以下方式:

public class YourFrame extends JFrame { 
    public YourFrame() { 
     ... new SwingWorkerProgressMonitor(this, ...); 
    } 
} 

public class SwingWorkerProgressMonitor extends ... { 
    private final JFrame frame; 

    public SwingWorkerProgressMonitor(JFrame frame, ...) { 
     this.frame = frame; 
     ... 
    } 

    private void setIconInProgress() { 
     this.frame.setIconImage(...); 
    } 
} 
+0

得到解決方案bobbel,非常感謝你...... :) – user2914906