java多线程实验 滚动字

Posted slgkaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程实验 滚动字相关的知识,希望对你有一定的参考价值。

package com.rgy.Test;

import java.awt.GridLayout;
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;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Test3 extends JFrame{
		//加入属性
		//
		private JPanel panel[];
		private JTextField tf[];
		private JLabel label_sleep[];
		private JTextField tf_sleep[];
		private JButton button_start[];
		private JButton button_interrupt[];
		private JLabel label_state[]; 
		private JTextField tf_state[];
		private MyThread_0 thread_0;
		private MyThread_1 thread_1;
		private MyThread_2 thread_2;
		//
		private String texts[]={"Welcome","Hello","Rolly"};
		//
		private int sleeptime0;
		private int sleeptime1;
		private int sleeptime2;
		public Test3(String str){ 
			super(str);
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setBounds(500, 250, 500, 320);	
			//加入功能代码
			this.setLayout(new GridLayout(6,1));
			tf=new JTextField[3];
			panel=new JPanel[3];
			label_sleep=new JLabel[3];
			tf_sleep=new JTextField[3];
			button_start=new JButton[3];
			button_interrupt=new JButton[3];
			label_state=new JLabel[3];
			tf_state=new JTextField[3];
			//
			for(int i=0;i<3;i++){
				panel[i]=new JPanel();
				tf[i]=new JTextField(String.format("%160s", texts[i]));
				tf[i].setHorizontalAlignment(JTextField.RIGHT);
				//
				label_sleep[i]=new JLabel("sleep");
				tf_sleep[i]=new JTextField(10);
				button_start[i]=new JButton("启动");
				button_interrupt[i]=new JButton("中断");
				label_state[i]=new JLabel("state");
				tf_state[i]=new JTextField(14);
				tf_state[i].setEditable(false);
				//
				panel[i].add(label_sleep[i]);
				panel[i].add(tf_sleep[i]);
				panel[i].add(button_start[i]);
				panel[i].add(button_interrupt[i]);
				panel[i].add(label_state[i]);
				panel[i].add(tf_state[i]);
				button_start[i].addActionListener(new button_handler());
				button_interrupt[i].addActionListener(new button_handler());
				this.add(tf[i]);
				this.add(panel[i]);
			}
			//
			this.pack();
			this.setVisible(true);
		}
		
		class MyThread_0 extends Thread{
			public void run(){
				while(true){
					try {
						String str=tf[0].getText();
						tf[0].setText(str.substring(1)+str.substring(0,1));
						sleep(sleeptime0);
					} catch (Exception e) {
						break;
					}
				}
			}
		}
		
		class MyThread_1 extends Thread{
			public void run(){
				while(true){
					try {
						String str=tf[1].getText();
						tf[1].setText(str.substring(1)+str.substring(0,1));
						sleep(sleeptime1);
					} catch (Exception e) {
						break;
					}
				}
			}
		}
		
		class MyThread_2 extends Thread{
			public void run(){
				while(true){
					try {
						String str=tf[2].getText();
						tf[2].setText(str.substring(1)+str.substring(0,1));
						sleep(sleeptime2);
					} catch (Exception e) {
						break;
					}
				}
			}
		}
		
		class button_handler implements ActionListener{
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==button_start[0]){
					sleeptime0=Integer.parseInt(tf_sleep[0].getText());
					thread_0=new MyThread_0();
					thread_0.start();
					button_start[0].setEnabled(false);
					button_interrupt[0].setEnabled(true);
					tf_state[0].setText(""+thread_0.getState());
				}
				if(e.getSource()==button_interrupt[0]){
					thread_0.interrupt();
					button_start[0].setEnabled(true);
					button_interrupt[0].setEnabled(false);
					tf_state[0].setText(""+thread_0.getState());
				}
				if(e.getSource()==button_start[1]){
					sleeptime1=Integer.parseInt(tf_sleep[1].getText());
					thread_1=new MyThread_1();
					thread_1.start();
					button_start[1].setEnabled(false);
					button_interrupt[1].setEnabled(true);
					tf_state[1].setText(""+thread_1.getState());
				}
				if(e.getSource()==button_interrupt[1]){
					thread_1.interrupt();
					button_start[1].setEnabled(true);
					button_interrupt[1].setEnabled(false);
					tf_state[1].setText(""+thread_1.getState());
				}
				if(e.getSource()==button_start[2]){
					sleeptime2=Integer.parseInt(tf_sleep[2].getText());
					thread_2=new MyThread_2();
					thread_2.start();
					button_start[2].setEnabled(false);
					button_interrupt[2].setEnabled(true);
					tf_state[2].setText(""+thread_2.getState());
				}
				if(e.getSource()==button_interrupt[2]){
					thread_2.interrupt();
					button_start[2].setEnabled(true);
					button_interrupt[2].setEnabled(false);
					tf_state[2].setText(""+thread_2.getState());
				}
			}
		}
		
		public static void main(String args[]) {
			new Test3("滚动字");
		}
}

技术分享

以上是关于java多线程实验 滚动字的主要内容,如果未能解决你的问题,请参考以下文章

Java如何实现多线程传输文件,就像迅雷下载一样,开十多个线程分段传送字节流?

关于java多线程死锁的实验

Java实验项目四——多线程矩阵相乘算法的设计

2016/9/25编写java实验报告时对synchronized(同步代码块)的一些感悟

[8w字] | Java多线程全套功法

双线程猜数字 (java多线程习题详述)