标签:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RollbyJFrame extends JFrame {
public RollbyJFrame(String []text1){
super("滚动字幕");
setBounds(300,240,500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
if(text1==null||text1.length==0){
getContentPane().add(new RollbyJpanel("welcome"));
}
getContentPane().setLayout(new GridLayout(text1.length,1));
for(int i=0;i<text1.length;i++){
getContentPane().add(new RollbyJpanel(text1[i]));
}
setVisible(true);
}
public static void main(String[] args) {
String text[]={"welcome","湖南城市学院","你好"};
new RollbyJFrame(text);
}
}
class RollbyJpanel extends JPanel implements ActionListener, Runnable{
private JTextField textsleeptime,textstate,textfiled;
private JButton btnsleep,btnstop;
private int sleeptime;
private Thread thread;
public RollbyJpanel(String text){
setLayout(new GridLayout(2,1));
char []space=new char[150];
Arrays.fill(space,‘ ‘);
textfiled=new JTextField(text+new String(space));
add(textfiled);
JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT));
add(panel);
panel.add(new JLabel("sleep"));
sleeptime=(int)(Math.random()*100);
textsleeptime=new JTextField(""+sleeptime,5);
panel.add(textsleeptime);
textsleeptime.addActionListener(this);
btnsleep=new JButton("启动");
panel.add(btnsleep);
btnsleep.addActionListener(this);
btnstop=new JButton("中断");
panel.add(btnstop);
btnstop.addActionListener(this);
panel.add(new JLabel("state"));
textstate=new JTextField("AAA",10);
textstate.setEditable(false);
panel.add(textstate);
}
public void run() {
while(true){
String str=textfiled.getText();
str=str.substring(1)+str.substring(0,1);
textfiled.setText(str);
try{
Thread.sleep(sleeptime);
}
catch(InterruptedException e){
break;
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnsleep){
int res=processSleeptime();
if(res==1){
return;
}
thread=new Thread(this);
thread.start();
btnsleep.setEnabled(false);
btnstop.setEnabled(true);
textstate.setText( ""+thread.getState());
}
if(e.getSource()==btnstop){
thread.interrupt();
btnstop.setEnabled(false);
btnsleep.setEnabled(true);
textstate.setText(""+thread.getState());
}
if(e.getSource()==textsleeptime){
processSleeptime();
}
}
private int processSleeptime(){
try{
sleeptime =Integer.parseInt(textsleeptime.getText());
return 0;}
catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this, "\""+textsleeptime.getText()+"\"不能输入睡眠时间,必须是整数");
return 1;
}
}
}
标签:
原文地址:http://www.cnblogs.com/1314wamm/p/5603430.html