我想展示在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 :(得分: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();
}