首页 > 代码库 > java多线程实验 滚动字
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("滚动字"); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。