首页 > 编程语言 > 详细

java多线程实验 滚动字

时间:2014-05-18 07:20:25      阅读:412      评论:0      收藏:0      [点我收藏+]
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("滚动字");
		}
}

bubuko.com,布布扣

java多线程实验 滚动字,布布扣,bubuko.com

java多线程实验 滚动字

原文:http://blog.csdn.net/u011700203/article/details/26060585

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!