import java.awt.FlowLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WelcomJFrame extends JFrame{
public WelcomJFrame(String[] texts) {
super("WelcomJFrame");//设置标题
setBounds(300, 240, 500, 400);//设置界面位置和大小
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置红色关闭按钮
getContentPane().setLayout(new GridLayout(texts.length, 1));//设置网格布局texts.length行,1列
//防护texts的空指针异常
if(texts==null || texts.length==0){
getContentPane().add(new RollbyPanel("welcom"));
}
for(int i=0;i<texts.length;i++){
getContentPane().add(new RollbyPanel( texts[i] ));//第二种多线程的创建方式
}
//显示框架
setVisible(true);
}
public static void main(String[] args) {
String texts[]={"Welcome","chengshixueyuan","hello"};
new WelcomJFrame(texts);
}
}
class RollbyPanel extends JPanel implements Runnable, ActionListener{
private JTextField tfdText,tfdSleepTime,tfdThreadState;//滚动字,睡眠时间,线程状态
private JButton btnStart,btnInterrupt;//启动按钮,中断按钮
private int sleepTime;//睡眠时间
private Thread threadRollby;
//对于每一个线程设计面板和功能
public RollbyPanel(String text) {
setLayout(new GridLayout(2, 1));
//方式1
// char spaces[]=new char[150-text.length()];
// Arrays.fill(spaces, ' ');//memset()
// tfdText =new JTextField(text+new String(spaces));
//方式2
String str=String.format("%150s", text);
tfdText =new JTextField(str);
add(tfdText);
JPanel panel=new JPanel( new FlowLayout(FlowLayout.LEFT));//对面板设置流布局并且左对齐
add(panel);//加面板
panel.add(new JLabel( "sleep" ));//标签
sleepTime=(int)(Math.random()*100);//睡眠时用随机数间赋初值
tfdSleepTime =new JTextField("" +sleepTime, 5);//对tfdSleepTime文本设置值,和文本长度
panel.add(tfdSleepTime);
btnStart =new JButton("启动");
panel.add(btnStart);
btnInterrupt =new JButton("中断");
btnInterrupt.setEnabled(false);//第一次设置 中断 按钮不可编辑
panel.add(btnInterrupt);
panel.add(new JLabel( "state" ));
tfdThreadState=new JTextField(10);//new并且设置tfdThreadState文本长度
tfdThreadState.setEditable(false);//设置tfdThreadState文本按钮不可编辑
panel.add(tfdThreadState);
//监听
btnStart.addActionListener(this);
btnInterrupt.addActionListener(this);
tfdSleepTime.addActionListener(this);
}
public void run() {
while (true) {
//使文本中的文字滚动
String str = tfdText.getText();
str = str.substring(1) + str.substring(0, 1);
tfdText.setText(str);
try {
Thread.sleep(sleepTime);//睡眠:即滚动的速度
} catch (InterruptedException e) {
break;//出异常就中断
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnStart){
threadRollby =new Thread(this);//当点击btnStart按钮时new一个线程
int r=processSleepTime();//判断此时的sleepTime能不能转换成睡眠时间
if(r==1){//如果leepTime不能转换成睡眠就中断,break用于循环,这里不能使用
return;
}
threadRollby.start();//调动run()方法
btnStart.setEnabled(false);
btnInterrupt.setEnabled(true);
tfdThreadState.setText(""+threadRollby.getState());//获得线程的状态,设置tfdThreadState文本
}
if(e.getSource() == btnInterrupt){
threadRollby.interrupt();//线程中断,结束
btnStart.setEnabled(true);
btnInterrupt.setEnabled(false);
tfdThreadState.setText(""+threadRollby.getState());//获得线程的状态,设置tfdThreadState文本
}
if(e.getSource()==tfdSleepTime){
processSleepTime();//设置滚动字的速度
}
}
private int processSleepTime() {//防护滚动字的速度
try {
sleepTime=Integer.parseInt(tfdSleepTime.getText());
return 0;
} catch (NumberFormatException e1) {
//防止在运行时,改变速度出错,中断运行
btnStart.setEnabled(true);
btnInterrupt.setEnabled(false);
threadRollby.interrupt();
JOptionPane.showMessageDialog(this, "\""+tfdSleepTime.getText()+"\"不能转换成睡眠时间 ");
tfdThreadState.setText(""+threadRollby.getState());
return 1;
}
}
}
Java--设计滚动字演示线程状态及改变方法
最新推荐文章于 2021-02-27 09:04:06 发布