疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java双线程滚动文字


 

   import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  public class WelcomeJFrame extends JFrame

  {

  public WelcomeJFrame(String texts[])

  {

  super("滚动字"); //标题

  this.setBounds(300,240,400,300); //设立组件坐标位置和宽度,高度

  this.setDefaultCloseOperation(EXIT_ON_CLOSE);

  if(texts==null||texts.length==0) //当数组为空长度为0,则至少要一个字符串

  this.getContentPane().add(new RollbyJPanel("Welcome")); //获得容器,新建一个RollbyJPanel的面板

  else

  {

  this.getContentPane().setLayout(new GridLayout(texts.length,1));//网格布局。this。length行,一列

  for(int i=0;i

  this.getContentPane().add(new RollbyJPanel(texts[i]));//获得容器,新建一个RollbyJPanel的面板

  }

  this.setVisible(true); //使窗体可见

  }

  public WelcomeJFrame() //

  {

  this(null);

  }

  private class RollbyJPanel extends JPanel implements ActionListener,Runnable

  {

  JTextField text_word,text_sleep,text_state;

  JButton button_start,button_interrupt;

  Thread thread;

  int sleeptime;

  RollbyJPanel(String text)

  {

  this.setLayout(new GridLayout(2,1));

  text_word=new JTextField(String.format("%115s",text));

  this.add(text_word);

  JPanel panel_sub=new JPanel();

  this.add(panel_sub);

  panel_sub.add(new JLabel("sleep"));

  this.sleeptime=(int)(Math.random()*100); //产生随机数作为间隔时间

  text_sleep=new JTextField(""+sleeptime,5);

  panel_sub.add(text_sleep);

  text_sleep.addActionListener(this);

  button_start=new JButton("启动");

  panel_sub.add(button_start);

  button_start.addActionListener(this);

  button_interrupt=new JButton("中断");

  panel_sub.add(button_interrupt);

  button_interrupt.addActionListener(this);

  thread=new Thread(this);

  button_interrupt.setEnabled(false);

  panel_sub.add(new JLabel("state"));

  text_state=new JTextField(""+thread.getState(),10);

  text_state.setEditable(false);

  panel_sub.add(text_state);

  }

  public void run()

  {

  while(true)

  try

  {

  String str=text_word.getText();

  text_word.setText(str.substring(1)+str.substring(0,1));

  Thread.sleep(sleeptime);

  }

  catch(InterruptedException ex)

  {

  break;

  }

  }

  public void actionPerformed(ActionEvent ev)

  {

  if(ev.getSource()==button_start)

  {

  thread=new Thread(this);

  thread.start();

  text_state.setText(""+thread.getState());

  button_start.setEnabled(false);

  button_interrupt.setEnabled(true);

  }

  if(ev.getSource()==button_interrupt)

  {

  thread.interrupt();

  text_state.setText(""+thread.getState());

  button_start.setEnabled(true);

  button_interrupt.setEnabled(false);

  }

  if(ev.getSource()==text_sleep)

  try

  {

  sleeptime=Integer.parseInt(text_sleep.getText());

  }

  catch(NumberFormatException nfex)

  {

  JOptionPane.showMessageDialog(this,"""+text_sleep.getText()+"" 不能转换成整数,请重新输出!");

  }

  }

  }

  public static void main(String args[])

  {

  String texts[]={"Welcome","Hello","Rollby"};

  new WelcomeJFrame(texts);

  }

  }