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();
}
}
修改了一下,可以勉强实现,还需修改!
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();
}
}
修改了一下,可以勉强实现,还需修改!
解决方案 »
- 请问这两种java类的实例化有什么区别?
- 来者有分!Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance()生成的实例有什么用?
- 如何用java获得子网掩码?
- 一读 MS-Sql 2000 数据库表转入二维容器的方法:ArrayList 和 Array
- double类问题
- 请大家看看这个程序
- 请教!!
- JDBC:ODBC怎样连接远程ODBC数据源?
- 问个问题,请大家帮帮忙啊!
- 大家down了没有呀?这本jbuilder的教程!!http://cnopensource.org/down/jbguide.zip
- 讨厌的字符数组和字符串的比较(简单)
- 请问
我没试
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();
}
}
既然是在Button1中加的监听就不用把Go()方法设为Public,直接写到里边就可以!你源程序的主要问题是你虽然加了按钮的事件,但在Main里边运行的只是你初始化的一部分。
建议你先把程序的结构弄清楚点。这要不仅思路可以清楚点 ,也可以更省气!!!加油