我已經編寫了此代碼來更新我的進度欄進度消息。基本上,當我有在()循環上運行的長度進程時,我將它放在循環中,並用prog = i和max = max數組更新它。使用更新(圖形)時Jlabel文本倍增和重疊
for(int i = 1; i <= array.length-1; i++){
//Process
setPrg(this.lbl, ths.prg, i, array.length-1, "Doing so process");
}
-
public void setPrg(JLabel lbl, JProgressBar prg, int prog, int max, String msg) {
prg.setMaximum(max);
if (prog <= max) {
prg.setValue(prog);
}
prg.update(prg.getGraphics());
if (prog >= max) {
setMessage(lbl, "");
} else {
setMessage(lbl, msg);
}
}
public void setMessage(JLabel lbl, String msg) {
lbl.setText(msg);
lbl.update(lbl.getGraphics());
}
我這樣做具有多個連續對()循環,其中每個具有描述過程獨特的消息。
問題是,由於某種原因,標籤文本自身重疊。舉例來說,如果我有一個循環運行此代碼:
setPrg(this.lbl, ths.prg, i, array.length-1, "This is process 1");
而且一個運行此代碼:
setPrg(this.lbl, ths.prg, i, array.length-1, "The second process this is");
然後作爲第二過程開始從兩個消息的文本將盡快重疊,而不是「這是過程1」消失。
在另一方面,如果我不包括這一行:
lbl.update(lbl.getGraphics());
無文本將在所有的循環中顯示,只有最後一個文本設置爲標籤會後出現循環。
非常感謝!
下面是完整的程序:
package test;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class Main extends javax.swing.JFrame {
public Main() {
initComponents();
}
public void setPrg(JLabel lbl, JProgressBar prg, int prog, int max, String msg) {
prg.setMaximum(max);
if (prog <= max) {
prg.setValue(prog);
}
prg.update(prg.getGraphics());
if (prog >= max) {
setMessage(lbl, "");
} else {
setMessage(lbl, msg);
}
}
public void setMessage(JLabel lbl, String msg) {
lbl.setText(msg);
lbl.update(lbl.getGraphics());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Button");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("jLabel1");
jTextField1.setText("jTextField1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(246, 246, 246)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(226, 226, 226)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(108, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(22, 22, 22)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 1; i <= 10000; i++) {
this.setPrg(jLabel1, jProgressBar1, i, 10000, "Message 1");
}
for (int i = 1; i <= 10000; i++) {
this.setPrg(jLabel1, jProgressBar1, i, 10000, "Second Message");
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JLabel jLabel1;
public javax.swing.JProgressBar jProgressBar1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}
'lbl.update(lbl.getGraphics())'是沒有意義的;你應該刪除它。無論如何,你的標籤是一個覆蓋paint或paintComponent方法的子類嗎? – VGR
「描述過程的唯一消息」你運行的線程少嗎? – Andrew
你的代碼是在EventDispatchThread上執行還是在WorkerThread上? – Alexander