2014-02-28 49 views
0

我想显示在java中使用进度监视器的程序进程的进度。我在下面的代码中将这段代码作为进度监视器显示Java中的进度监视器实现

package eksim.view; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.ProgressMonitor; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 
import javax.swing.UIManager; 


public class ProgBar extends javax.swing.JInternalFrame implements ActionListener { 

    static ProgressMonitor pbar; 
    static int counter = 0; 

    /** 
    * Creates new form ProgBar 
    */ 
    public ProgBar() { 
     initComponents(); 
    } 

    public void ProgressMonitorExample() { 
    super("Progress Monitor Demo"); 
    setSize(250, 100); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    pbar = new ProgressMonitor(null, "Monitoring Progress", 
     "Initializing . . .", 0, 100); 

    // Fire a timer every once in a while to update the progress. 
    Timer timer = new Timer(500, this); 
    timer.start(); 
    setVisible(true); 
    } 

    public static void main(String args[]) { 
    UIManager.put("ProgressMonitor.progressText", "This is progress?"); 
    UIManager.put("OptionPane.cancelButtonText", "Go Away"); 
    ProgressMonitorExample(); 
    } 

    public void actionPerformed(ActionEvent e) { 
    // Invoked by the timer every half second. Simply place 
    // the progress monitor update on the event queue. 
    SwingUtilities.invokeLater(new Update()); 
    } 

    class Update implements Runnable { 
    public void run() { 
     if (pbar.isCanceled()) { 
     pbar.close(); 
     System.exit(1); 
     } 
     pbar.setProgress(counter); 
     pbar.setNote("Operation is " + counter + "% complete"); 
     counter += 2; 
    } 
    } 


    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jProgressBar1 = new javax.swing.JProgressBar(); 

     setClosable(true); 
     setIconifiable(true); 
     setMaximizable(true); 
     setTitle("Progress Monitor"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(31, 31, 31) 
       .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(28, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(66, Short.MAX_VALUE) 
       .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(48, 48, 48)) 
     ); 

     pack(); 
    }// </editor-fold>       
    // Variables declaration - do not modify      
    private javax.swing.JProgressBar jProgressBar1; 
    // End of variables declaration     
} 

但是,它没有显示任何东西,因为它的工作原理。任何人都可以帮我吗?谢谢

+0

的代码将无法编译。您可以从静态上下文中调用ProgressMonitorExample()。 – PeterMmm

回答

0

除了你的代码中的一些编译问题(详细如下),它在这里工作正常。进度条按照预期在此处更新。

首先,将代码从public void ProgressMonitorExample()移到构造函数中,并删除该方法。构造函数应该是这样的:

public ProgBar() { 
     super("Progress Monitor Demo"); 
     initComponents(); 
    setSize(250, 100); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    pbar = new ProgressMonitor(null, "Monitoring Progress", 
     "Initializing . . .", 0, 100); 

    // Fire a timer every once in a while to update the progress. 
    Timer timer = new Timer(500, this); 
    timer.start(); 
    setVisible(true); 
    } 

其次,由于public void ProgressMonitorExample()现在已经不复存在了,正确地创建一个新的ProgBar对象,而不是:

public static void main(String args[]) { 
    UIManager.put("ProgressMonitor.progressText", "This is progress?"); 
    UIManager.put("OptionPane.cancelButtonText", "Go Away"); 
    ProgBar pb = new ProgBar(); 
    } 
+0

我采取你的指示,但我感到困惑。我什么时候可以打电话给这个ProgBar?当我点击该项目的另一个视图中的按钮时,我必须打电话吗? – syaloom