今天在写一个用共享变量中止进程,共享变量可以变,但进程中止不了,请各们指点一下,谢谢了。附源码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.IOException;
import java.util.Date;
import java.lang.*;
import java.util.Timer;class urltest extends Thread{
public boolean isStop=true;
public void run(){
while(isStop){
try{
//System.out.println("Body:"+isStop);
String url="http://localhost/javasp/index.asp?name=wxf888&pass=eeee";
URL Url1=new URL(url);
URLConnection URLconnection=Url1.openConnection();
URLconnection.connect();
System.out.println("内容类型"+URLconnection.getContentType());
}
catch(IOException e){
System.out.println(e);
}
try{
sleep(1000);
}
catch(InterruptedException e){
System.out.println(e);
}
}
}
public void setisStop(){
isStop=false;
}
}public class DgLib extends JFrame implements ActionListener
{
private JTextField MoJtf=new JTextField("上");
private JTextField MtJtf=new JTextField("下");
private JLabel MoJLbl=new JLabel("上");
private JLabel MtJLbl=new JLabel("下");
private JButton StartBtton=new JButton("开始");
private JButton StopButton=new JButton("停止");
private Container container; public DgLib(){
StartBtton.addActionListener(this);
StopButton.addActionListener(this);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout()); jp.add(MoJLbl);
jp.add(MoJtf);
jp.add(MtJLbl);
jp.add(MtJtf);
jp.add(StartBtton);
jp.add(StopButton);
/*this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});*/
container=getContentPane();
container.add(jp);
this.setSize(300,100);
this.setVisible(true);
} public void actionPerformed(ActionEvent e){
urltest Myurltest=new urltest();
if (e.getSource()==StartBtton){
try{
Myurltest.start();
}
catch(Exception ee){
System.out.println("Damn"+ee);
}
}
if(e.getSource()==StopButton){
try{
//urltest Myurltest2=new urltest();
Myurltest.setisStop();
//System.out.println(Myurltest.isStop);
}
catch(Exception ee){
System.out.println("Damn"+ee);
}
}
} public static void main(String[] args){
DgLib MyLib=new DgLib();
MyLib.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.IOException;
import java.util.Date;
import java.lang.*;
import java.util.Timer;class urltest extends Thread{
public boolean isStop=true;
public void run(){
while(isStop){
try{
//System.out.println("Body:"+isStop);
String url="http://localhost/javasp/index.asp?name=wxf888&pass=eeee";
URL Url1=new URL(url);
URLConnection URLconnection=Url1.openConnection();
URLconnection.connect();
System.out.println("内容类型"+URLconnection.getContentType());
}
catch(IOException e){
System.out.println(e);
}
try{
sleep(1000);
}
catch(InterruptedException e){
System.out.println(e);
}
}
}
public void setisStop(){
isStop=false;
}
}public class DgLib extends JFrame implements ActionListener
{
private JTextField MoJtf=new JTextField("上");
private JTextField MtJtf=new JTextField("下");
private JLabel MoJLbl=new JLabel("上");
private JLabel MtJLbl=new JLabel("下");
private JButton StartBtton=new JButton("开始");
private JButton StopButton=new JButton("停止");
private Container container; public DgLib(){
StartBtton.addActionListener(this);
StopButton.addActionListener(this);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout()); jp.add(MoJLbl);
jp.add(MoJtf);
jp.add(MtJLbl);
jp.add(MtJtf);
jp.add(StartBtton);
jp.add(StopButton);
/*this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});*/
container=getContentPane();
container.add(jp);
this.setSize(300,100);
this.setVisible(true);
} public void actionPerformed(ActionEvent e){
urltest Myurltest=new urltest();
if (e.getSource()==StartBtton){
try{
Myurltest.start();
}
catch(Exception ee){
System.out.println("Damn"+ee);
}
}
if(e.getSource()==StopButton){
try{
//urltest Myurltest2=new urltest();
Myurltest.setisStop();
//System.out.println(Myurltest.isStop);
}
catch(Exception ee){
System.out.println("Damn"+ee);
}
}
} public static void main(String[] args){
DgLib MyLib=new DgLib();
MyLib.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
这句:urltest Myurltest=new urltest(); 转移到DgLib的成员变量那里