Thread在内部类中的使用

good_shi 2008-02-18 02:19:02
private JButton getClassNameButton() {
if (classNameButton == null) {
classNameButton = new JButton();
classNameButton.setText("运行应用程序");
classNameButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
/*if(!(run.isAlive())){
run = new Thread(new ExciseExec());
}*/
try{
run.start();
}catch(Exception e1){

}
//card.show(getAreaPanel(),"dosTextArea");
}
});
}
return classNameButton;
}
该JButton内部类中的Thread类实现时用this参数不行,如果用自身主类(new ExciseExec())参数则重新创建了一个JFram的试图,相应的Run方法
public void run() {
System.out.println("run");
if(Thread.currentThread() == compiler){
getCompilerTextArea().setText("");
String temp = getInputTextArea().getText().trim();
System.out.println("temp :"+temp);
byte[] b = temp.getBytes();
int leng = b.length;
String fileName = getFileNameTextField().getText().trim();

try {
loadFile = new File(fileName);
String name = loadFile.getAbsolutePath();
System.out.println("name"+name);
FileOutputStream fos = new FileOutputStream(loadFile);
fos.write(b,0,leng);
fos.close();
} catch (IOException e) {}
}
却不能执行if内部的循环,请问各位大侠如何解决这个问题!
...全文
88 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
good_shi 2008-02-20
  • 打赏
  • 举报
回复
呵呵,程序有点大,不方便全部贴出,不过谢谢高手指点,问题解决了,不知道可以这样用this.
deng4437 2008-02-18
  • 打赏
  • 举报
回复
你的程序没有全部贴出来,根据个人经验,你就应该用你类名+this来做你的Thread的构造方法的参数.如下:
Thread run = new Thread(ExciseExec.this);
这是必须的,为什么这里不能用简单的用"this",那是因为你在构造run的时候,是把它内部类中.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧