Java实验-Swing 简单多线程实验

实验要求:

多线程是生活中常见的现象,我们要让计算机程序同时做N件事,就可以通过多线程实现:

如图所示,分别用Thread和Runnable两种方法各开2条线程,实现如下界面,每条线程的数字都会数数字,从A0-A100,B0-B100,C0-C100,D0-D100,周而复始。

请用Java图形界面的知识,编写以上软件。

代码:

--JavaMain.java文件中的三个class:

package com.junlin.exer9;
import com.junlin.exer9.MyFrame;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextField;
import java.awt.Color;

/**
 * @Description:
 * @author: junlin623
 * @create: 2022-11-20 23:44
 */

public class JavaMain {

    public static void main(String[] args) {
        MyFrame frame = new MyFrame("xxx");

        MyThread T_A = new MyThread("A", frame.ttext1, Color.blue, 0.5);
        MyThread T_B = new MyThread("B", frame.ttext2, Color.green, 0.3);
        MyRunnable R_A = new MyRunnable("A", frame.rtext1, Color.red, 0.6);
        MyRunnable R_B = new MyRunnable("B", frame.rtext2, Color.gray, 0.8);

        T_A.start();
        T_B.start();
        new Thread(R_A).start();
        new Thread(R_B).start();

    }
}
//自定义继承Thread类的线程类
class MyThread extends Thread {
    private String name;  //线程名称
    private JTextField ascription;  //这个线程控制的文本控件
    private int cnt = 0;  //计数
    private Color textColor;  //文本颜色
    private double gap;  //每次刷新的时间间隔
    public MyThread(String name, JTextField ascription, Color color, double gap) {
        this.name = name;
        this.ascription = ascription;
        this.cnt = 0;
        this.textColor = color;
        this.ascription.setForeground(this.textColor);
        this.gap = gap;
    }
    @Override
    public void run() {
        while(true) {
            //System.out.println("Thread" + this.name + ":" + this.cnt);
            this.ascription.setText("Thread" + this.name + ":" + this.cnt);
            //休眠指定时间,这样可以让每个文本框速度不同的更新
            try {
                Thread.sleep((int)(this.gap * 1000));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //使用这行代码可以立即刷新控件的内容,而不用回到主线程再刷新
            this.ascription.paintImmediately(this.ascription.getBounds());
            cnt = (cnt + 1) % 100;
        }

    }
}

//实现Runnable接口的线程类(和继承方式相似)
class MyRunnable implements Runnable {
    private String name;
    private JTextField ascription;
    private int cnt = 0;
    private Color textColor;
    private double gap;
    public MyRunnable(String name, JTextField ascription, Color color, double gap) {
        this.name = name;
        this.ascription = ascription;
        this.cnt = 0;
        this.textColor = color;
        this.ascription.setForeground(this.textColor);
        this.gap = gap;
    }
    @Override
    public void run() {
        while(true) {
//       System.out.println("Runnable" + this.name + ":" + this.cnt);
            this.ascription.setText("Runnable" + this.name + ":" + this.cnt);
            try {
                Thread.sleep((int)(this.gap * 1000));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.ascription.paintImmediately(this.ascription.getBounds());
            cnt = (cnt + 1) % 100;
        }
    }
}

--窗体类MyFrame

package com.junlin.exer9;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 * @Description:
 * @author: junlin623
 * @create: 2022-11-20 23:45
 */
public class MyFrame extends JFrame{
    public JLabel title1;
    public JLabel title2;
    public JTextField ttext1;  //左上角文本控件
    public JTextField ttext2;  //右上角文本控件
    public JTextField rtext1; //左下角文本控件
    public JTextField rtext2;  //右下角文本控件
    public MyFrame(String name) {
        super(name);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);
        JPanel thread_panel = new JPanel(null);
        thread_panel.setBackground(Color.white);
        JPanel runnable_panel = new JPanel(null);
        runnable_panel.setBackground(Color.white);

        ttext1 = new JTextField("Thread A:0");
        ttext1.setBounds(50,60,120,30);
        ttext1.setFont(new Font("黑体", Font.BOLD, 16));
        thread_panel.add(ttext1);
        ttext2 = new JTextField("Thread B:0");
        ttext2.setFont(new Font("黑体", Font.BOLD, 16));
        ttext2.setBounds(300,60,120, 30);
        thread_panel.add(ttext2);
        rtext1 = new JTextField("Runnable A:0");
        rtext1.setFont(new Font("黑体", Font.BOLD, 16));
        rtext1.setBounds(50,60,120, 30);
        runnable_panel.add(rtext1);
        rtext2 = new JTextField("Runnable B:0");
        rtext2.setBounds(300,60,120, 30);
        rtext2.setFont(new Font("黑体", Font.BOLD, 16));
        runnable_panel.add(rtext2);
        //给panel添加一个边框
        Border border1 = BorderFactory.createTitledBorder("Thread方法的2个线程");
        Border border2 = BorderFactory.createTitledBorder("Runnable方法的2个线程");
        thread_panel.setBounds(10,25,550, 250);
        thread_panel.setBorder(border1);
        runnable_panel.setBorder(border2);
        runnable_panel.setBounds(10, 275, 550 ,250);

        contentPane.add(thread_panel);
        contentPane.add(runnable_panel);
        this.setVisible(true);
        this.setSize(new Dimension(600,600));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


posted @ 2023-03-02 13:18  junlin623  阅读(117)  评论(0编辑  收藏  举报