Java 简单多线程选号

每位数字用一个线程控制其变化,点击“停止”按钮则所有标签数字停止变化。



方式一:不同线程产生不同数字,将数字替换容器中JLable的值


package eight;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class task5 {

	public static void main(String[] args) {
		new JFrameTest();
	}
}

class JFrameTest extends JFrame implements ActionListener {

	private JButton jButton;
	private JLabel[] jLabels;
	private JPanel jPanel;
	private ThreadMain threadMain;

	public JFrameTest() {
		// 加入基本组件
		setBounds(150, 100, 400, 400);

		jButton = new JButton("停止");
		jButton.addActionListener(this);
		jLabels = new JLabel[5];
		add(jButton, BorderLayout.NORTH);

		jPanel = new JPanel();
		for (int i = 0; i < 5; i++) {
			jLabels[i] = new JLabel();
			jPanel.add(jLabels[i]);
		}
		add(jPanel, BorderLayout.CENTER);

		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		// 启动线程
		threadMain = new ThreadMain(this);
		threadMain.start();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		threadMain.myStop();
	}

	public JLabel[] getjLabels() {
		return jLabels;
	}

	public void setjLabels(int[] tempNumber) {
		// 将随机数重新赋值个lable
		for(int i =0;i < 5; i++) {
			this.jLabels[i].setText(tempNumber[i] + "");
		}
	}
}

class ThreadMain extends Thread {
	private ThreadSon[] threadSons;
	private boolean flag;
	private JFrameTest jFrameTest;

	public ThreadMain(JFrameTest jFrameTest) {
		flag = true;
		this.jFrameTest = jFrameTest;
		threadSons = new ThreadSon[5];
		// 启动子线程
		for (int i = 0; i < 5; i++) {
			threadSons[i] = new ThreadSon();
			threadSons[i].start();
		}
	}

	public void run() {
		while (flag) {
			int[] tempNumber = new int[5];
			for (int i = 0; i < 5; i++) {
				tempNumber[i] = threadSons[i].getNumber();
			}
			jFrameTest.setjLabels(tempNumber);
		}
	}

	public void myStop() {
		// 关闭当前线程
		flag = false;
		// 关闭子线程
		for (ThreadSon element : threadSons) {
			element.setFlag(false);
		}
	}

}

class ThreadSon extends Thread {
	private int number;
	private boolean flag;

	public ThreadSon() {
		flag = true;
	}
//  产生随随机数线程
	public void run() {
		while (flag) {
			number = (int)(Math.random() * 10);
//			System.out.print(number);
		}
	}

	public int getNumber() {
		return number;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}

方式二: 让JLable 继承Thread,随后直接对JLable的run()方法中进行操作


package eight;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class task6 {
	public static void main(String[] args) {
		new JFrameNumber();
	}
}

class JFrameNumber extends JFrame implements ActionListener {

	private JButton jButton;
	private MyJLbale[] myJLbales;
	private JPanel jPanel;

	public JFrameNumber() {
		// 加入基本组件
		setBounds(150, 100, 400, 400);

		jButton = new JButton("停止");
		jButton.addActionListener(this);
		add(jButton, BorderLayout.NORTH);

		jPanel = new JPanel();
		myJLbales = new MyJLbale[5];
		for (int i = 0; i < 5; i++) {
			myJLbales[i] = new MyJLbale();
			jPanel.add(myJLbales[i]);
			// 启动线程
			new Thread(myJLbales[i]).start();
		}
		add(jPanel, BorderLayout.CENTER);

		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for (int i = 0; i < 5; i++) {
			myJLbales[i].setFlag(false);
		}
	}
}

// 线程标签
class MyJLbale extends JLabel implements Runnable {

	private boolean flag;

	public MyJLbale() {
		setText("0");
		flag = true;
	}

	@Override
	public void run() {
		while (flag) {
//			System.out.println(Thread.currentThread().getName());
//			System.out.println(getText());
			setText("" + (int) (Math.random() * 10));
		}
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值