java進度條,java進度條不動

本文目錄一覽:

JAVA 進度條

直接上官方示例

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.beans.*;

import java.util.Random;

 

public class ProgressBarDemo extends JPanel

                             implements ActionListener,

                                        PropertyChangeListener {

 

    private JProgressBar progressBar;

    private JButton startButton;

    private JTextArea taskOutput;

    private Task task;

 

    class Task extends SwingWorkerVoid, Void {

        /*

         * Main task. Executed in background thread.

         */

        @Override

        public Void doInBackground() {

            Random random = new Random();

            int progress = 0;

            //Initialize progress property.

            setProgress(0);

            while (progress  100) {

                //Sleep for up to one second.

                try {

                    Thread.sleep(random.nextInt(1000));

                } catch (InterruptedException ignore) {}

                //Make random progress.

                progress += random.nextInt(10);

                setProgress(Math.min(progress, 100));

            }

            return null;

        }

 

        /*

         * Executed in event dispatching thread

         */

        @Override

        public void done() {

            Toolkit.getDefaultToolkit().beep();

            startButton.setEnabled(true);

            setCursor(null); //turn off the wait cursor

            taskOutput.append(“Done!\n”);

        }

    }

 

    public ProgressBarDemo() {

        super(new BorderLayout());

 

        //Create the demo’s UI.

        startButton = new JButton(“Start”);

        startButton.setActionCommand(“start”);

        startButton.addActionListener(this);

 

        progressBar = new JProgressBar(0, 100);

        progressBar.setValue(0);

        progressBar.setStringPainted(true);

 

        taskOutput = new JTextArea(5, 20);

        taskOutput.setMargin(new Insets(5,5,5,5));

        taskOutput.setEditable(false);

 

        JPanel panel = new JPanel();

        panel.add(startButton);

        panel.add(progressBar);

 

        add(panel, BorderLayout.PAGE_START);

        add(new JScrollPane(taskOutput), BorderLayout.CENTER);

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

 

    }

 

    /**

     * Invoked when the user presses the start button.

     */

    public void actionPerformed(ActionEvent evt) {

        startButton.setEnabled(false);

        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        //Instances of javax.swing.SwingWorker are not reusuable, so

        //we create new instances as needed.

        task = new Task();

        task.addPropertyChangeListener(this);

        task.execute();

    }

 

    /**

     * Invoked when task’s progress property changes.

     */

    public void propertyChange(PropertyChangeEvent evt) {

        if (“progress” == evt.getPropertyName()) {

            int progress = (Integer) evt.getNewValue();

            progressBar.setValue(progress);

            taskOutput.append(String.format(

                    “Completed %d%% of task.\n”, task.getProgress()));

        }

    }

 

 

    /**

     * Create the GUI and show it. As with all GUI code, this must run

     * on the event-dispatching thread.

     */

    private static void createAndShowGUI() {

        //Create and set up the window.

        JFrame frame = new JFrame(“ProgressBarDemo”);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

        //Create and set up the content pane.

        JComponent newContentPane = new ProgressBarDemo();

        newContentPane.setOpaque(true); //content panes must be opaque

        frame.setContentPane(newContentPane);

 

        //Display the window.

        frame.pack();

        frame.setVisible(true);

    }

 

    public static void main(String[] args) {

        //Schedule a job for the event-dispatching thread:

        //creating and showing this application’s GUI.

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }

}

Java中如何實現進度條效果

import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; @SuppressWarnings(“serial”) public class Demo extends JWindow implements Runnable { // 定義加載窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 獲取屏幕窗口大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 定義進度條組件 public JProgressBar progressbar; // 定義標籤組件 public JLabel label; // 構造函數 public Demo() { // 創建標籤,並在標籤上放置一張圖片 label = new JLabel(new ImageIcon(“images/background.jpg”)); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT – 15); // 創建進度條 progressbar = new JProgressBar(); // 顯示當前進度值信息 progressbar.setStringPainted(true); // 設置進度條邊框不顯示 progressbar.setBorderPainted(false); // 設置進度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設置進度條的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT – 15, LOAD_WIDTH, 15); // 添加組件 this.add(label); this.add(progressbar); // 設置布局為空 this.setLayout(null); // 設置窗口初始位置 this.setLocation((WIDTH – LOAD_WIDTH) / 2, (HEIGHT – LOAD_HEIGHT) / 2); // 設置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 設置窗口顯示 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } @Override public void run() { for (int i = 0; i 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, “加載完成”); this.dispose(); } }

java多線程導出如何更新進度條

1:一個子進程進行計算

2:計算過程中返回計算的百分比,比如10%,20%。。。。。100%;

3:前端需要一個定時器不斷請求進度,然後更新進度條。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/190485.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-29 22:33
下一篇 2024-11-29 22:33

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論