帮我改一下这两道中断程序,能够启动,但是不能中断。麻烦修改过的发上来,谢谢!import java.awt.*;
import java.awt.event.*;
public class Welcome extends WindowAdapter implements ActionListener
{
Frame f;
static Welcome.Thread3 wt1,wt2;
public static void main(String arg[])
{
Welcome w= new Welcome();
w.display();
wt1=w.new Thread3("Welcome!");
wt2=w.new Thread3("How are you?");
wt2.start();
wt2.setButton(); //设置按钮状态
}
public void display()
{
f = new Frame("Welcome");
f.setSize(400,240);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(4,1));
f.addWindowListener(this);
f.setVisible(true);
}
public class Thread3 extends Thread
{
Panel p1;
Label lb1;
TextField tf1,tf2;
Button b1,b2;
int sleeptime = (int)(Math.random()*100);
public Thread3(String str)
{
super(str);
for(int i=0;i<100;i++)
str = str + " ";
tf1 = new TextField(str);
f.add(tf1);
p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
lb1 = new Label("sleep");
tf2 = new TextField(""+sleeptime);
p1.add(lb1);
p1.add(tf2);
b1 = new Button("启动");
b2 = new Button("中断");
p1.add(b1);
p1.add(b2);
b1.addActionListener(new Welcome());
b2.addActionListener(new Welcome());
f.add(p1);
f.setVisible(true);
}
public void run()
{
String str;
while(true)
{
// while (this.isAlive() && !this.isInterrupted())
// { //线程活动且没中断时
try
{
str = tf1.getText();
str = str.substring(1)+ str.substring(0,1);
tf1.setText(str);
this.sleep(sleeptime);
}
catch(InterruptedException e)
{ //中断时抛出
System.out.println(e);
//break; //退出循环
}
}
}
public void setButton() //设置按钮状态
{
if (this.isAlive()) b1.setEnabled(false);
else b1.setEnabled(true);
if (this.isInterrupted()) b2.setEnabled(false);
else b2.setEnabled(true);
}
}//线程
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{ //单击按钮时触发
if ((e.getSource()==wt1.b1) || (e.getSource()==wt1.b2))
actionPerformed(e,wt1);
if ((e.getSource()==wt2.b1) || (e.getSource()==wt2.b2))
actionPerformed(e,wt2);
}
public void actionPerformed(ActionEvent e,Thread3 wt1)
{ //重载
if(e.getSource()==wt1.b1) //启动
{
wt1.sleeptime=Integer.parseInt(wt1.tf2.getText());
wt1.start();
}
if(e.getSource()==wt1.b2) //中断
wt1.interrupt();
wt1.setButton(); //设置按钮状态
}
}
这是另外一道import java.awt.*;
import java.awt.event.*;
public class 暂停线程 extends Frame implements Runnable,ActionListener{
Frame f;
Button b1,b2,b3,b4;
Label l;
int x,y;
Thread T;
Boolean Tag; ///应该改为boolean
static 暂停线程 nc;
public static void main(String[] args) {
nc=new 暂停线程();
nc.display();
}
public void display(){
f=new Frame();
f.setLayout(null);
b1=new Button("开始");
b1.setBounds(20, 20, 50, 30);
b2=new Button("暂停");
b2.setBounds(80,20,50,30);
b1.addActionListener(this);
b2.addActionListener(this);
b1.setEnabled(true);
b2.setEnabled(false);
f.add(b1);
f.add(b2);
l=new Label("我是标签");
l.setBounds(20,60,50,50);
f.add(l);
f.setBounds(10, 10, 300, 200); //窗体大小
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e1){
if (e1.getSource()==b1){
try{
T=new Thread(nc);
T.start();
}
catch(Exception e3){}
Tag=true;
b1.setEnabled(false);
b2.setEnabled(true);
}
if(e1.getSource()==b2){
Tag=false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public void run(){
while(true)
{
if(Tag){
x=l.getBounds().x;
y=l.getBounds().y;
x=x+2;
y=y+2;
if (x>250) x=20;
if (y>150) y=60;
l.setLocation(x,y);
try
{
T.sleep(200);
}
catch(Exception e2){}
}
}
}
}
import java.awt.event.*;
public class Welcome extends WindowAdapter implements ActionListener
{
Frame f;
static Welcome.Thread3 wt1,wt2;
public static void main(String arg[])
{
Welcome w= new Welcome();
w.display();
wt1=w.new Thread3("Welcome!");
wt2=w.new Thread3("How are you?");
wt2.start();
wt2.setButton(); //设置按钮状态
}
public void display()
{
f = new Frame("Welcome");
f.setSize(400,240);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.setLayout(new GridLayout(4,1));
f.addWindowListener(this);
f.setVisible(true);
}
public class Thread3 extends Thread
{
Panel p1;
Label lb1;
TextField tf1,tf2;
Button b1,b2;
int sleeptime = (int)(Math.random()*100);
public Thread3(String str)
{
super(str);
for(int i=0;i<100;i++)
str = str + " ";
tf1 = new TextField(str);
f.add(tf1);
p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
lb1 = new Label("sleep");
tf2 = new TextField(""+sleeptime);
p1.add(lb1);
p1.add(tf2);
b1 = new Button("启动");
b2 = new Button("中断");
p1.add(b1);
p1.add(b2);
b1.addActionListener(new Welcome());
b2.addActionListener(new Welcome());
f.add(p1);
f.setVisible(true);
}
public void run()
{
String str;
while(true)
{
// while (this.isAlive() && !this.isInterrupted())
// { //线程活动且没中断时
try
{
str = tf1.getText();
str = str.substring(1)+ str.substring(0,1);
tf1.setText(str);
this.sleep(sleeptime);
}
catch(InterruptedException e)
{ //中断时抛出
System.out.println(e);
//break; //退出循环
}
}
}
public void setButton() //设置按钮状态
{
if (this.isAlive()) b1.setEnabled(false);
else b1.setEnabled(true);
if (this.isInterrupted()) b2.setEnabled(false);
else b2.setEnabled(true);
}
}//线程
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{ //单击按钮时触发
if ((e.getSource()==wt1.b1) || (e.getSource()==wt1.b2))
actionPerformed(e,wt1);
if ((e.getSource()==wt2.b1) || (e.getSource()==wt2.b2))
actionPerformed(e,wt2);
}
public void actionPerformed(ActionEvent e,Thread3 wt1)
{ //重载
if(e.getSource()==wt1.b1) //启动
{
wt1.sleeptime=Integer.parseInt(wt1.tf2.getText());
wt1.start();
}
if(e.getSource()==wt1.b2) //中断
wt1.interrupt();
wt1.setButton(); //设置按钮状态
}
}
这是另外一道import java.awt.*;
import java.awt.event.*;
public class 暂停线程 extends Frame implements Runnable,ActionListener{
Frame f;
Button b1,b2,b3,b4;
Label l;
int x,y;
Thread T;
Boolean Tag; ///应该改为boolean
static 暂停线程 nc;
public static void main(String[] args) {
nc=new 暂停线程();
nc.display();
}
public void display(){
f=new Frame();
f.setLayout(null);
b1=new Button("开始");
b1.setBounds(20, 20, 50, 30);
b2=new Button("暂停");
b2.setBounds(80,20,50,30);
b1.addActionListener(this);
b2.addActionListener(this);
b1.setEnabled(true);
b2.setEnabled(false);
f.add(b1);
f.add(b2);
l=new Label("我是标签");
l.setBounds(20,60,50,50);
f.add(l);
f.setBounds(10, 10, 300, 200); //窗体大小
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e1){
if (e1.getSource()==b1){
try{
T=new Thread(nc);
T.start();
}
catch(Exception e3){}
Tag=true;
b1.setEnabled(false);
b2.setEnabled(true);
}
if(e1.getSource()==b2){
Tag=false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public void run(){
while(true)
{
if(Tag){
x=l.getBounds().x;
y=l.getBounds().y;
x=x+2;
y=y+2;
if (x>250) x=20;
if (y>150) y=60;
l.setLocation(x,y);
try
{
T.sleep(200);
}
catch(Exception e2){}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货