线程实在搞不定了!!!help!!!

setnever 2004-06-10 11:46:13
/*
我想向您请教一个关于JAVA线程的问题,就是我的这个程序运行时什么反应也没有,我想让一个文本框里面的数每10毫秒自增加1,可是当START按下去之后却起不来了,看了好长时间却不知道是什么地方出错了,感激不尽!!!!
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class dis extends JFrame
{
JTextField t;
JButton b1;
JButton b2;
Thread th;
public boolean runFlag = true;
int count = 0;
public dis()
{
try
{
b1=new JButton();
b2=new JButton();
b1.addActionListener(new on());
b2.addActionListener(new off());
b1.setText("START");
b2.setText("STOP");
t=new JTextField(10);
th=new Thread();
th.start();
this.getContentPane().setLayout(null);
t.setSize(130,30);
b1.setBounds(2,50,80,20);
b2.setBounds(89,50,70,20);
getContentPane().add(t);
getContentPane().add(b1);
getContentPane().add(b2);
this.setBounds(100,100,190,130);
this.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void go()
{
for(int a=1;;a++)
{
try {
if(runFlag==true)
{
t.setText(Integer.toString(a));
}
th.sleep(100);
}
catch (Exception t)
{
System.out.println(t.getMessage());
}

}
}
class on implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
go();
}
}
class off implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runFlag=!runFlag;
}
}
}


public class mythread
{
public static void main(String args[])
{
new dis();
}
}
...全文
88 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhushizu 2004-06-11
  • 打赏
  • 举报
回复
是呀,好乱的程序。。。。
既然是在Button1中加的监听就不用把Go()方法设为Public,直接写到里边就可以!

你源程序的主要问题是你虽然加了按钮的事件,但在Main里边运行的只是你初始化的一部分。
建议你先把程序的结构弄清楚点。这要不仅思路可以清楚点 ,也可以更省气!!!

加油
killme2008 2004-06-11
  • 打赏
  • 举报
回复
这样改行不行?
我没试
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class dis extends JFrame implements Runnable
{
JTextField t;
JButton b1;
JButton b2;
Thread th;
public boolean runFlag = true;
int count = 0;
public dis()
{
try
{
b1=new JButton();
b2=new JButton();
b1.addActionListener(new on());
b2.addActionListener(new off());
b1.setText("START");
b2.setText("STOP");
t=new JTextField(10);
th=new Thread();
th.start();
this.getContentPane().setLayout(null);
t.setSize(130,30);
b1.setBounds(2,50,80,20);
b2.setBounds(89,50,70,20);
getContentPane().add(t);
getContentPane().add(b1);
getContentPane().add(b2);
this.setBounds(100,100,190,130);
this.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void run()
{
for(int a=1;;a++)
{
try {
if(runFlag==true)
{
t.setText(Integer.toString(a));
}
th.sleep(100);
}
catch (Exception t)
{
System.out.println(t.getMessage());
}

}
}
class on implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
new Thread(this).start();
}
}
class off implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(runflag=true)
runFlag=!runFlag;
}
}
}


public class mythread
{
public static void main(String args[])
{
new dis();
}
}

梦想家起飞 2004-06-11
  • 打赏
  • 举报
回复
原程序居然有两个内部类,程序设计的合理性需要考虑哦.

还有那一段代码应该分别放在两个文件吧.
林仪明 2004-06-11
  • 打赏
  • 举报
回复
你的程序不能运行的问题如下:你试图在事件线程中实现后台工作线程,很明显你不清楚事件线程的工作机制。有关事件线程的工作机制网上有很多啦!我就不说啦!

以上网友的改法都是使程序的后台工作线程和事件线程分开运行。
happyegg 2004-06-10
  • 打赏
  • 举报
回复
你这个根本就不是线程嘛。你要另外写一个class MyThread extends Thread, 在那里面做go的事情才行的
liangyongwen 2004-06-10
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


class dis extends JFrame implements Runnable
{
JTextField t;
JButton b1;
JButton b2;
Thread th;
public boolean runFlag = true;
int count = 0;
public dis()
{
try
{
b1=new JButton();
b2=new JButton();
b1.addActionListener(new on());
b2.addActionListener(new off());
b1.setText("START");
b2.setText("STOP");
t=new JTextField(10);
th=new Thread();

this.getContentPane().setLayout(null);
t.setSize(130,30);
b1.setBounds(2,50,80,20);
b2.setBounds(89,50,70,20);
getContentPane().add(t);
getContentPane().add(b1);
getContentPane().add(b2);
this.setBounds(100,100,190,130);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void run()
{
for(int a=1;;a++)
{
try {
if(runFlag==true)
{
t.setText(Integer.toString(a));
}
th.sleep(100);
}
catch (Exception t)
{
System.out.println(t.getMessage());
}

}
}
class on implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
new Thread(dis.this).start();
}
}
class off implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runFlag=false;
}
}
}


public class Test1
{
public static void main(String args[])
{
new dis();
}
}
修改了一下,可以勉强实现,还需修改!

62,623

社区成员

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

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