日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA多线程程序ProgressBar

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA多线程程序ProgressBar 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JAVA多線程程序ProgressBar

題目簡介:

思維導圖:

實驗代碼:建議先看CalThread類,計算線程的實現,再作基本CalFrame類的界面,

????????????????? 然后作ReadThread類,結合CalFrame的組件,最后完善CalFrame類

?(代碼折疊)

import java.awt.EventQueue;import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.JLabel; import java.awt.Font; import javax.swing.SwingConstants; import javax.swing.JPanel; import java.awt.GridLayout; import javax.swing.JTextArea; import javax.swing.JProgressBar; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /***@author 李祖林*/ public class CalFrame implements ActionListener{private JFrame frame;JTextArea textA,textB;JProgressBar progressBar;JButton button;public CalFrame() {frame = new JFrame();frame.setBounds(100, 100, 755, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new BorderLayout(0, 0));JLabel label = new JLabel("\u591A\u7EBF\u7A0B \u8BA1\u7B971!+2!+...+12!");label.setHorizontalAlignment(SwingConstants.CENTER);label.setFont(new Font("宋體", Font.BOLD, 18));frame.getContentPane().add(label, BorderLayout.NORTH);JPanel panel = new JPanel();frame.getContentPane().add(panel, BorderLayout.CENTER);panel.setLayout(new GridLayout(3, 2, 0, 0));JLabel label_1 = new JLabel("\u8BA1\u7B97\u8FC7\u7A0B");label_1.setFont(new Font("宋體", Font.BOLD, 18));label_1.setHorizontalAlignment(SwingConstants.CENTER);panel.add(label_1);textA = new JTextArea();textA.setFont(new Font("Courier New", Font.BOLD, 15));panel.add(textA);JLabel label_2 = new JLabel("\u8FDB\u5EA6\u6761");label_2.setHorizontalAlignment(SwingConstants.CENTER);label_2.setFont(new Font("宋體", Font.BOLD, 18));panel.add(label_2);progressBar = new JProgressBar();progressBar.setMaximum(12);panel.add(progressBar);JLabel label_3 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");label_3.setHorizontalAlignment(SwingConstants.CENTER);label_3.setFont(new Font("宋體", Font.BOLD, 18));panel.add(label_3);textB = new JTextArea();textB.setFont(new Font("Courier New", Font.BOLD, 18));panel.add(textB);JPanel panel_1 = new JPanel();frame.getContentPane().add(panel_1, BorderLayout.SOUTH);button = new JButton("\u5F00\u59CB");button.setFont(new Font("宋體", Font.BOLD, 18));panel_1.add(button);button.addActionListener(this);frame.setVisible(true);}public void actionPerformed(ActionEvent e) {/*計算線程 自啟動*/CalThread calThread = new CalThread();/*讀取線程*/ReadThread readThread = new ReadThread(this); //此處的this是CalFrame類的對象Thread thread = new Thread(readThread); //非自啟動:構造擴展線程類的對象,通過Thread()傳該類得以實現線程 thread.start();}public static void main(String[] args) {CalFrame calFrame = new CalFrame();}} CalFrame /*** @author 李祖林**/ public class ReadThread extends Thread {/*成員變量*/CalFrame calFrame;/*(有參與無參)構造函數*/public ReadThread() {}public ReadThread(CalFrame calFrame) {this.calFrame = calFrame;}/*線程開始讀取*/public void run(){while(true){calFrame.textA.setText(CalThread.string); //計算過程calFrame.textB.setText(String.valueOf(CalThread.sum)); //計算結果calFrame.progressBar.setValue(CalThread.n); //計算進度try {Thread.sleep(100);} catch (InterruptedException e) { System.err.println("讀取線程ReadThread發送錯誤!");e.printStackTrace();}}} } ReadThread /*** @author 李祖林**/ public class CalThread extends Thread {static double sum = 1; static String string = "1!";static int n = 1;Thread thread = null;/*構造函數 自啟動*/public CalThread() {thread = new Thread(this);thread.start();}/*計算n!*/double fun(int n){double sum = 1;for(int i = 1;i <= n;i++){sum *= i;}return sum;}/*線程開始計算*/public void run(){while(n<12){n++; //進度條進度sum += fun(n); //計算結果string = string + "+" + n + "!"; //計算過程try {Thread.sleep((int)Math.random()*600 + 300); //隨機300-900毫秒} catch (InterruptedException e) {System.err.println("計算線程CalThread出現錯誤!");e.printStackTrace();}}}} CalThread

?實驗結果:

轉載于:https://www.cnblogs.com/jdemarryme/p/7070216.html

總結

以上是生活随笔為你收集整理的JAVA多线程程序ProgressBar的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。