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(); } }

/** * @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(); } } } }

/** * @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(); } } } }
實驗結果: