【问题标题】:Progress Monitor Implementation in JavaJava中的进度监视器实现
【发布时间】:2014-02-28 07:25:20
【问题描述】:

我想显示我在 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                   
}

但是,它在工作时没有显示任何内容。有人可以帮我吗?谢谢

【问题讨论】:

  • 代码无法编译。您从静态上下文中调用 ProgressMonitorExample()

标签: java progress-bar dialog


【解决方案1】:

除了代码中的一些编译问题(下文详述)外,它在这里运行良好。进度条在此处按预期更新。

首先,将代码从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();
  }

【讨论】:

  • 我接受了你的指示,但我很困惑。我什么时候可以调用这个 ProgBar?当我从项目的另一个视图单击按钮时,我必须调用它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2017-11-28
相关资源
最近更新 更多