我写了个记事本
在里面用一个线程来在文本域中显示时间
此时我要读入文本 该怎么停止这个线程?
package textedit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class TXTFrame extends JFrame{
public TXTFrame(){
panel=new TXTPanel();
add(panel);
setTitle("记事本");
Toolkit kit=getToolkit();
Dimension screen=kit.getScreenSize();
int width=screen.width;
int height=screen.height;
setBounds(width/4,height/4,width/2,height/2);
file=new JMenu("文件");
edit=new JMenu("编辑");
m1=new JMenuItem("新建");
m2=new JMenuItem("打开");
m3=new JMenuItem("保存");
m4=new JMenuItem("另存为") ;
m5=new JMenuItem("退出");
m6=new JMenuItem("撤销");
m7=new JMenuItem("剪切");
m8=new JMenuItem("复制");
m9=new JMenuItem("粘贴");
m10=new JMenuItem("删除");
m11=new JMenuItem("查找");
m12=new JMenuItem("查找下一个");
m13=new JMenuItem("替换");
m14=new JMenuItem("转到");
m15=new JMenuItem("全选");
m16=new JMenuItem("时间/日期");
m1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
panel=new TXTPanel();
add(panel);
}
});
m2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
/*setBoolean(false);
setThread();*/
i=false;
chooser.setCurrentDirectory(new File("E:\\me"));
chooser.setFileFilter(filter);
//chooser.setAcceptAllFileFilterUsed(false);
//chooser.setSelectedFile(new File("1.txt"));
//System.out.print(JFileChooser.CANCEL_OPTION);
int ss=chooser.showOpenDialog(TXTFrame.this);
//System.out.print(ss);
if(ss==JFileChooser.APPROVE_OPTION){
String s,s2=new String(),str;
try{
//str=chooser.getCurrentDirectory().toString()+"\\"+chooser.getSelectedFile().getName();
in=new BufferedReader(new FileReader(chooser.getSelectedFile().getPath()));
while((s=in.readLine())!=null)
s2+=s+"\n";
in.close();
panel.getJText().setText(s2);
}catch(IOException e){
e.printStackTrace();
}
}
/*if(ss==JFileChooser.CANCEL_OPTION){
panel.text.setText(chooser.getCurrentDirectory().toString());
}*/
}
});
m3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
/*setBoolean(false);
setThread();*/
chooser.setCurrentDirectory(new File("E:\\me"));
chooser.setFileFilter(filter);
chooser.setAcceptAllFileFilterUsed(false);
int ss=chooser.showSaveDialog(TXTFrame.this);
if(ss==JFileChooser.APPROVE_OPTION){
String s;
s=panel.getJText().getText();
//System.out.print(s);
try{
String str=chooser.getSelectedFile().getPath()+filter.getDescription();
out=new BufferedWriter(new FileWriter(str));
out.write(s);
out.close();
}catch(IOException e){
}
}
}
});
m4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
/*setBoolean(false);
setThread();*/
chooser.setCurrentDirectory(new File("E:\\me"));
chooser.setFileFilter(filter);
int ss=chooser.showSaveDialog(TXTFrame.this);
if(ss==JFileChooser.APPROVE_OPTION){
String s=panel.getJText().getText();
try{
String str=chooser.getSelectedFile().getPath()+filter.getDescription();
out=new BufferedWriter(new FileWriter(str));
out.write(s);
out.close();
}catch(IOException e){
}
}
}
});
m5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
m16.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(!d.isAlive()){
i=true;
d.start();
}/*else if(d.isAlive()){
i=true;
}*/
}
});
file.add(m1);
file.add(m2);
file.add(m3);
file.add(m4);
file.add(new JSeparator());
file.add(m5);
edit.add(m6);
edit.add(new JSeparator());
edit.add(m7);
edit.add(m8);
edit.add(m9);
edit.add(m10);
edit.add(new JSeparator());
edit.add(m11);
edit.add(m12);
edit.add(m13);
edit.add(m14);
edit.add(new JSeparator());
edit.add(m15);
edit.add(m16);
menubar=new JMenuBar();
menubar.add(file);
menubar.add(edit);
setJMenuBar(menubar);
}
private class DateThread extends Thread{
public void run(){
if(i){
while(i){
panel.getJText().setText(new Date().toString());
try{
Thread.sleep(100);
}catch(InterruptedException e){
}
}
}else{
return;
}
}
}
/*public void setBoolean(boolean i){
this.i=i;
}
public void setThread(){
if ( i ) {
if(d.isAlive()&&d.){
d.notify();
}else{
d.start();
}
}
else {
try{
if(d.isAlive())
d.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
*/
private JMenuItem m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16;
private TXTPanel panel;
private JMenuBar menubar;
private JMenu file,edit;
private JFileChooser chooser=new JFileChooser();
private File f;
private BufferedReader in;
private BufferedWriter out;
private TXTFilter filter=new TXTFilter();
private boolean i=false;
private DateThread d=new DateThread();
}