2016-12-14 79 views
1

我已經編寫了此代碼來更新我的進度欄進度消息。基本上,當我有在()循環上運行的長度進程時,我將它放在循環中,並用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     
} 
+1

'lbl.update(lbl.getGraphics())'是沒有意義的;你應該刪除它。無論如何,你的標籤是一個覆蓋paint或paintComponent方法的子類嗎? – VGR

+0

「描述過程的唯一消息」你運行的線程少嗎? – Andrew

+1

你的代碼是在EventDispatchThread上執行還是在WorkerThread上? – Alexander

回答

2

您的問題是在Swing事件線程上運行長時間運行的代碼以及進行無效的Swing圖形調用的經典問題。解決方案是在後臺線程中執行長時間運行的代碼,例如SwingWorker的doInBackground方法,並注意在Swing事件線程上進行所有Swing調用。

所有這一切都在,很容易找到教程是很好的解釋,包括

例如,編譯和運行這個:

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import java.util.concurrent.ExecutionException; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class Main4b extends JPanel { 
    private static final int MAX = 10000; 
    private static final int PREF_W = 400; 
    private static final int PREF_H = 200; 
    private ButtonAction buttonAction = new ButtonAction(); 
    private JButton button = new JButton(buttonAction); 
    private JTextField jTextField1 = new JTextField("jTextField1", 10); 
    private JLabel jLabel1 = new JLabel("jLabel1"); 
    private JProgressBar jProgressBar1 = new JProgressBar(0, MAX); 

    public Main4b() { 
     jProgressBar1.setStringPainted(true); 

     setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); 
     add(new JPanel() {{add(button);}}); 
     add(new JPanel() {{add(jTextField1);}}); 
     add(new JPanel() {{add(jLabel1);}}); 
     add(jProgressBar1); 
     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     Dimension superSz = super.getPreferredSize(); 
     if (isPreferredSizeSet()) { 
      return superSz; 
     } 
     int prefW = Math.max(superSz.width, PREF_W); 
     int prefH = Math.max(superSz.height, PREF_H); 
     return new Dimension(prefW, prefH); 
    } 

    private class ButtonAction extends AbstractAction { 
     public ButtonAction() { 
      super("Button"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      setEnabled(false); // make the button non-pressable 

      // create background thread worker 
      MyWorker myWorker = new MyWorker(); 

      // add propertychange listener to it 
      myWorker.addPropertyChangeListener(new WorkerListener()); 

      // run the worker thread 
      myWorker.execute(); 
     } 
    } 

    // background thread 
    private static class MyWorker extends SwingWorker<Void, Integer> { 
     private static final String MY_PROGRESS = "my progress"; 
     private int myProgress = 0; 

     @Override 
     protected Void doInBackground() throws Exception { 
      // all this code is run in a background thread 

      // do this twice 
      for (int j = 0; j < 2; j++) { 

       // iterate from 0 to 10,000 
       for (int i = 0; i < MAX; i++) { 
        Thread.sleep(1); // small delay so we can see what we're doing 
        setMyProgress(i + j * MAX); // send output to listeners 
       } 
      } 
      return null; 
     } 

     public int getMyProgress() { 
      return myProgress; 
     } 

     // myProgress is a "bound" field, one that will notify listeners 
     // if it is changed 
     public void setMyProgress(int myProgress) { 
      int oldValue = this.myProgress; 
      int newValue = myProgress; 
      this.myProgress = myProgress; 
      // notify all listeners 
      firePropertyChange(MY_PROGRESS, oldValue, newValue); 
     } 

    } 

    private class WorkerListener implements PropertyChangeListener { 
     private String message = "Message %d: %05d"; 

     public WorkerListener() { 
      jLabel1.setText(String.format(message, 1, 0)); 
     } 

     @Override 
     public void propertyChange(PropertyChangeEvent evt) { 
      // all this code is run on the Swing event thread 

      // listen for changes to my progress bound field 
      if (MyWorker.MY_PROGRESS.equals(evt.getPropertyName())) { 
       int value = (int) evt.getNewValue(); // get value 
       int newMsgIndex = 1 + value/MAX; // message number 
       jProgressBar1.setValue(value % MAX); // set value on progress bar 
       jLabel1.setText(String.format(message, newMsgIndex, value % MAX)); 
      } else if (evt.getNewValue() == SwingWorker.StateValue.DONE) { 
       // worker is done 
       jLabel1.setText(""); // reset JLabel 
       buttonAction.setEnabled(true); // re-enable JButton's Action 
       MyWorker myWorker = (MyWorker) evt.getSource(); 
       try { 
        // always call this to catch and handle any exceptions that 
        // may have been thrown from within the worker 
        myWorker.get(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

    private static void createAndShowGui() { 
     Main4b mainPanel = new Main4b(); 

     JFrame frame = new JFrame("Main"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 
4

當您正在使用的Swing你自動在多線程環境。一個是主線程和Swing使用EDT進行UI重繪。

使用SwingUtilities.invokeLater()執行JProgressBar.setValue()JLabel.setValue()在EventDispatchThread。這確保它們將相應地更新。

示例如何使用SwingUtilities的片段。

final JProgressBar prg = new JProgressBar(); 
    final JLabel lbl = new JLabel(); 

    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      prg.setValue(70); 
      lbl.setText("Text"); 
     } 
    }); 

你會看到,你的呼叫lbl.update(lbl.getGraphics());不再需要

+0

我試過了,但它仍然沒有更新...所以現在進度條並沒有顯示任何變化 – Zhiming010

+0

'public void setPrg(final JLabel lbl,final JProgressBar prg,final int prog,final int max,final String MSG){ SwingUtilities.invokeLater(新的Runnable(){ @Override 公共無效的run(){ prg.setMaximum(最大值); 如果(PROG <=最大值){ prg.setValue(PROG); }如果(prog> = max)其他{ lbl。的setText(MSG); } } }); }' – Zhiming010