每位数字用一个线程控制其变化,点击“停止”按钮则所有标签数字停止变化。
方式一:不同线程产生不同数字,将数字替换容器中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;
}
}