创建两个线程,每个线程的工作都是在自己的界面的TextField区域中从左到右动态地显示一个字符串|JAVA
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了创建两个线程,每个线程的工作都是在自己的界面的TextField区域中从左到右动态地显示一个字符串|JAVA,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2306字,纯文字阅读大概需要4分钟。
内容图文
package packMutil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Muti1 extends WindowAdapter implements ActionListener
{
public static JFrame f = new JFrame("Welcome");
public static MyThread mt1 = new MyThread("Welcome!");
public static MyThread mt2 = new MyThread("How are you?");
public static void main(String args[])
{
Muti1 ltr = new Muti1();
ltr.dispaly();
mt1.start();
mt1.setButton();
mt2.start();
mt2.setButton();
}
public void dispaly()
{
f.setSize(400,240);
f.setLocation(200,140);
f.setBackground(Color.LIGHT_GRAY);
f.setLayout(new GridLayout(4,1));
f.addWindowListener(this);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if((e.getSource()==mt1.aButton)||(e.getSource()==mt1.bButton))
{
actionPerformed(e,mt1);
}
if((e.getSource()==mt2.aButton)||(e.getSource()==mt2.bButton))
{
actionPerformed(e,mt2);
}
}
public void actionPerformed(ActionEvent e,MyThread mt)
{
if(e.getSource() == mt.aButton)
{
mt.SleepTime = Integer.parseInt(mt.bField.getText());
mt.setButton();
}
if(e.getSource() == mt.bButton)
{
mt.interrupt();
mt.setButton();
}
}
}
package packMutil;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String;
public class MyThread extends Thread {
JPanel p;
JLabel alabel;
JTextField aField;
JTextField bField;
JButton aButton;
JButton bButton;
//随机睡眠时间
int SleepTime = (int)(Math.random()*100);
//构造函数
public MyThread(String str)
{
super(str); //调用父类的含参构造函数
for(int i=0;i<100;i++)
{
str = str +" ";
}
aField = new JTextField(str);
Muti1.f.add(aField);
p = new JPanel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
alabel = new JLabel("Sleep");
bField = new JTextField(""+SleepTime);
p.add(alabel);
p.add(bField);
aButton = new JButton("启动");
bButton = new JButton("中断");
p.add(aButton);
p.add(bButton);
aButton.addActionListener(new Muti1());
bButton.addActionListener(new Muti1());
Muti1.f.add(p);
Muti1.f.setVisible(true);
}
public void run()
{
String str;
while(true)
{
try
{
str = aField.getText();
str = str.substring(1)+str.substring(0,1);
aField.setText(str);
this.sleep(SleepTime);
}catch(InterruptedException e){
break;
}
}
}
public void setButton()
{
if(this.isAlive())
{
bButton.setEnabled(true);
aButton.setEnabled(false);
}
if(this.interrupted())
{
aButton.setEnabled(true);
bButton.setEnabled(false);
}
}
}
内容总结
以上是互联网集市为您收集整理的创建两个线程,每个线程的工作都是在自己的界面的TextField区域中从左到右动态地显示一个字符串|JAVA全部内容,希望文章能够帮你解决创建两个线程,每个线程的工作都是在自己的界面的TextField区域中从左到右动态地显示一个字符串|JAVA所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。