进度条Java

我正在使用JProgressBar显示进度。但是,如何显示ProgressBar从0加载到100?我从互联网上获取了代码,并且其工作正常,但progressBar未加载。



progressFrame = new JFrame(); // frame to display progress bar

progressBar = new JProgressBar(0,100);

progressBar.setValue(0);

progressBar.setStringPainted(true);

progressFrame.add(progressBar);


new SwingWorker<Void,Void>()

    {

        protected Void doInBackground() throws SQLException, ClassNotFoundException

        {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            progressBar.setValue(0);

            frame.setEnabled(false); // frame = main frame


            //tableclass creates a JTable with data from database

            tableclass = new TheDatabaseTable(deptName);//it takes time to create

            progressBar.setValue(50);

            frame.getContentPane().removeAll();

            frame.setContentPane(tableclass);


            frame.validate();

            frame.repaint();


            progressBar.setValue(100);

            //progressFrame.dispose();

            return null;

        }; 

        protected void done()

        {

            //progressFrame.setVisible(false);

            frame.setVisible(true);

            progressFrame.dispose();

            frame.setEnabled(true);

        }


    }.execute();

如果有人可以编辑上面的代码,我将不胜感激。谢谢。


慕娘9325324
浏览 379回答 3
3回答

万千封印

您必须为此使用线程。设计一个实现Runnable接口的类,该接口将更新这样的值。class ProgressBarUpdator implements java.lang.Runnable {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Progress bar that shows the current status&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private javax.swing.JProgressBar jpb = null;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Progress bar value&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private java.lang.Integer value = null;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructor&nbsp; &nbsp; &nbsp;* @param jpb The progress bar this has to update&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public ProgressBarUpdator(javax.swing.JProgressBar jpb) {&nbsp; &nbsp; &nbsp; &nbsp; this.jpb = jpb;&nbsp; &nbsp; &nbsp; &nbsp; jpb.setMaximum(100);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Sets the value to the progress bar&nbsp; &nbsp; &nbsp;* @param value Value to set&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void setValue(java.lang.Integer value) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Action of the thread will be executed here. The value of the progress bar will be set here.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jpb.setValue((int)java.lang.Math.round(java.lang.Math.floor(value.intValue() * 100 / maximum)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; java.lang.Thread.sleep(100L);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (java.lang.InterruptedException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while (value == null || value.intValue() < jpb.getMaximum());&nbsp; &nbsp; }}并在您的框架类中progressBar与新类一起使用ProgressBarUpdator ju = new ProgressBarUpdator(progressBar);new java.lang.Thread(ju).start();每当您想更改值时,只需使用以下语句ju.setValue([Value to set]);

白板的微信

一定要从网上获得爱情代码...哦...您拥有的代码违反了Swing的单一线程规则,因此是一个不好的例子。您可以使用多种选择SwingWorker。您可以publish使用进度并使用该process方法来更新进度条,也可以使用PropertyChangeListener和监视通过调用的setProgress方法创建的进度更改事件。SwingWorkerimport java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.SwingWorker;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.border.EmptyBorder;public class SwingWorkerProgress {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new SwingWorkerProgress();&nbsp; &nbsp; }&nbsp; &nbsp; public SwingWorkerProgress() {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Testing");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(new TestPane());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public class TestPane extends JPanel {&nbsp; &nbsp; &nbsp; &nbsp; private JProgressBar pbProgress;&nbsp; &nbsp; &nbsp; &nbsp; private JButton start;&nbsp; &nbsp; &nbsp; &nbsp; public TestPane() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setBorder(new EmptyBorder(10, 10, 10, 10));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pbProgress = new JProgressBar();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setLayout(new GridBagLayout());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GridBagConstraints gbc = new GridBagConstraints();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gbc.insets = new Insets(4, 4, 4, 4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gbc.gridx = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gbc.gridy = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(pbProgress, gbc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = new JButton("Start");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gbc.gridy++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(start, gbc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start.setEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProgressWorker pw = new ProgressWorker();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pw.addPropertyChangeListener(new PropertyChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void propertyChange(PropertyChangeEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = evt.getPropertyName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (name.equals("progress")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int progress = (int) evt.getNewValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pbProgress.setValue(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (name.equals("state")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (state) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case DONE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start.setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pw.execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class ProgressWorker extends SwingWorker<Object, Object> {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Object doInBackground() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int max = 2000;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i < max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int progress = Math.round(((float)i / (float)max) * 100f);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setProgress(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(25);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕莱坞森

import java.awt.GridBagLayout;import javax.swing.JFrame;import javax.swing.JProgressBar;public class ProgressBar {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* JProgressBar&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @throws InterruptedException&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args) throws InterruptedException {&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("New Window");&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(600, 400);&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(new GridBagLayout());&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; //JLabel textLoad = new JLabel();&nbsp; &nbsp; &nbsp; &nbsp; //textLoad.setText("Loading ....");&nbsp; &nbsp; &nbsp; &nbsp; JProgressBar progressBar = new JProgressBar();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; progressBar.setMinimum(0);&nbsp; &nbsp; &nbsp; &nbsp; progressBar.setMaximum(100);&nbsp; &nbsp; &nbsp; &nbsp; progressBar.setStringPainted(true);&nbsp; &nbsp; &nbsp; &nbsp; progressBar.setIndeterminate(true);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(progressBar);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = progressBar.getMinimum(); i <= progressBar.getMaximum(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressBar.setValue(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; textLoad.setText("Done !");&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java