zoukankan      html  css  js  c++  java
  • 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("u591Au7EBFu7A0B 20u4E2Au968Fu673Au6574u6570u7684u548C");
            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("u8BA1u7B97u7B49u5F0F
    ");
            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("u8FDBu5EA6u6761");
            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("u8BA1u7B97u7ED3u679C");
            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("u5F00u59CB");
            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;
    sum = 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

    实验结果:

  • 相关阅读:
    如何写好 5000 行的 SQL 代码
    Oracle面对“数据倾斜列使用绑定变量”场景的解决方案
    OAuth2.0最简向导(多图预警)
    再见,2019!你好,2020!
    快过年了,来,来,来!给七大姑八大姨好好解释解释【啥是DBA 】
    linux 定期清除日志
    人工智能:才赢李世石,再“战”巴菲特
    人工智能:才赢李世石,再“战”巴菲特
    人工智能:才赢李世石,再“战”巴菲特
    人工智能:才赢李世石,再“战”巴菲特
  • 原文地址:https://www.cnblogs.com/jdemarryme/p/7070801.html
Copyright © 2011-2022 走看看