JAVA多线程程序ProgressBar2

JAVA多线程程序ProgressBar2

题目简介:

思路分析:与上一篇:JAVA多线程程序ProgressBar类似,本篇避免过于冗杂,所以在此没有给出。

实验代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.activation.ActivationInstantiator;

import javax.swing.JTextArea;
import javax.swing.JProgressBar;
import java.awt.Font;
import javax.swing.JButton;

/**
 * @author 李祖林
 *
 */
public class SumFrame implements ActionListener{

    private JFrame frame;
    JTextArea textA,textB;
    JButton button;
    JProgressBar progressBar;

    public static void main(String[] args) {
        SumFrame sumFrame = new SumFrame();
    }

    
    public SumFrame() {
        
        frame = new JFrame();
        frame.setBounds(100, 100, 1043, 210);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));
        
        JLabel label = new JLabel("\u591A\u7EBF\u7A0B 20\u4E2A\u968F\u673A\u6574\u6570\u7684\u548C");
        label.setFont(new Font("宋体", Font.BOLD, 18));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        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\u7B49\u5F0F\r\n");
        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, 10));
        panel.add(textA);
        
        JLabel label_2 = new JLabel("\u8FDB\u5EA6\u6761");
        label_2.setFont(new Font("宋体", Font.BOLD, 18));
        label_2.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label_2);
        
        progressBar = new JProgressBar();
        progressBar.setMaximum(20);
        panel.add(progressBar);
        
        JLabel label_3 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");
        label_3.setFont(new Font("宋体", Font.BOLD, 18));
        label_3.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label_3);
        
        textB = new JTextArea();
        textB.setFont(new Font("Courier New", Font.BOLD, 15));
        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) {
        SumThread sumThread = new SumThread();
        
        ReadThread readThread = new ReadThread(this);
        Thread thread = new Thread(readThread);
        thread.start();
    }

}
SumFrame
/**
 * @author 李祖林
 *
 */
public class ReadThread extends Thread {
    SumFrame sumFrame;
    
    ReadThread(){}
    ReadThread(SumFrame sumFrame){
        this.sumFrame = sumFrame;
    }
    
    public void run(){
        while(true){
            sumFrame.textA.setText(SumThread.string);
            sumFrame.progressBar.setValue(SumThread.n);
            sumFrame.textB.setText(String.valueOf(SumThread.sum));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.err.println("读取线程ReadThread发生错误!");
                e.printStackTrace();
            }
        }
    }
}
ReadThread
/**
 * @author 李祖林
 *
 */
public class SumThread extends Thread{
    static double sum = 0;
    static int n = 0;
    static String string = "sum = 0;\nsum = 0";
    int tmp;
    
    SumThread(){
        Thread thread = new Thread(this);
        thread.start();
    }
    
    public void run(){
        while(n<20){
            n++;
            tmp = (int)(Math.random()*1000);
            sum += tmp;
            string = string + "+" + tmp;
            try {
                Thread.sleep((int)Math.random()*600 + 300);
            } catch (InterruptedException e) {
                System.err.println("计算线程发生错误!");
                e.printStackTrace();
            }
        }
    }
}
SumThread

实验结果:

 

posted @ 2017-06-23 16:59  霜沐林  阅读(557)  评论(0编辑  收藏  举报