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

 实验结果:

posted @ 2017-06-23 15:37  霜沐林  阅读(2570)  评论(0编辑  收藏  举报