想做一个applet
在applet里面有一个textfield 还有一个按钮
点击按钮之后textfield每隔1秒随机显示一个数字
再点击按钮就停止显示这样的效果怎么做出来?
我现在是点完按钮生成一个线程刷新textfield里面的数字,可是进到start()里面开始while循环刷新数字之后 那个按钮就不弹起来了 textfield 也没有显示东西,但是console却能够打印出来那些随机数字, 应该是他没从while里面跳出来吧? 我该怎么做呢?谢谢
在applet里面有一个textfield 还有一个按钮
点击按钮之后textfield每隔1秒随机显示一个数字
再点击按钮就停止显示这样的效果怎么做出来?
我现在是点完按钮生成一个线程刷新textfield里面的数字,可是进到start()里面开始while循环刷新数字之后 那个按钮就不弹起来了 textfield 也没有显示东西,但是console却能够打印出来那些随机数字, 应该是他没从while里面跳出来吧? 我该怎么做呢?谢谢
解决方案 »
- 十进制小数转换二进制算法
- bmp图片读取与修改,C中的用结构体读取,java中简单的方法是什么,哪位大哥能帮帮我呢
- 为什么说【移位运算】在运行时比普通乘除法效率高?如何用移位实现求余数(%)的运算?
- 誰能能提供一個鎖定鍵盤某個鍵的方法?
- ant的一个问题
- 搞JAVA的大多会不会其他语言
- 连接access数据库
- 我安装了jdk1.4,JAVA_HOME也设了,在cmd里面编译时报错:Exception in thread "main" java.lang.NoClassDefFoundError
- 一个怪问题,突然发现hello world不行了,还调试不通.汗ing
- 雷老虎,速来:)
- 推荐几本JAVA图形设计入门与提高方面的书籍
- 帮我看一看错在那里
import java.applet.Applet;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.Vector;public class Test extends Applet {
private final long serialVersionUID = 1L;
private Rander rander;
private JTextArea outputSpace = null; private JButton Start = null; private JTextField randDisplay = null; private int stop = 0; private Vector col; private ResultSet result; private Statement stmt; private Connection conn; private String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=AnBangProject"; private String user = "sa"; private String password = "anbang"; private String query = "select card_id from CardIdPsd"; /**
* This is the default constructor
*/
public Test() { super();
rander=new Rander(this);
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
result = stmt.executeQuery(query);
col = new Vector();
while (result.next()) {
col.add(result.getString("card_id"));
}
System.out.println("加载数据共"+col.size()+"条");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
this.setSize(600, 300); this.add(getRandDisplay(), null);
this.add(getOutputSpace(), null);
this.add(getStart(), null);
} /**
* This method initializes outputSpace
*
* @return javax.swing.JTextArea
*/
public JTextArea getOutputSpace() {
if (outputSpace == null) {
outputSpace = new JTextArea();
// outputSpace.setText("test");
outputSpace.setBackground(Color.lightGray);
outputSpace.setLineWrap(true);
outputSpace.setPreferredSize(new Dimension(600, 200));
// outputSpace.setAutoscrolls(true);
}
return outputSpace;
} /**
* This method initializes Start
*
* @return javax.swing.JButton
*/
private JButton getStart() {
if (Start == null) {
Start = new JButton();
Start.setText("Start");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { //System.out.println("actionPerformed()" + col.size());
rander.start();
//rander.yield(); }
});
}
return Start;
}
/**
* This method initializes randDisplay
*
* @return javax.swing.JTextField
*/
public JTextField getRandDisplay() {
if (randDisplay == null) {
randDisplay = new JTextField();
randDisplay.setPreferredSize(new Dimension(200, 22));
randDisplay.setBackground(Color.yellow);
}
return randDisplay;
}}===================================================================import java.util.Random;public class Rander extends Thread {
private Test thetest;
public Rander(Test t) {
thetest=t;
} public void start() {
//while(true){
Random r =new Random ();
//System.out.println(""+r.nextInt());
thetest.getRandDisplay().setText(""+r.nextInt());
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
}}
================
//ackage Award;
import java.applet.Applet;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.Timer;
import java.util.Vector;public class Test extends Applet {
private final long serialVersionUID = 1L;
private Rander rander;
private JTextArea outputSpace = null;
private Timer timer;
private JButton Start = null; private JTextField randDisplay = null; private int stop = 0; private Vector col; private ResultSet result; private Statement stmt; private Connection conn; private String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=AnBangProject"; private String user = "sa"; private String password = "anbang"; private String query = "select card_id from CardIdPsd"; /**
* This is the default constructor
*/
public Test() { super();
rander=new Rander(this);
timer=new Timer();
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
result = stmt.executeQuery(query);
col = new Vector();
while (result.next()) {
col.add(result.getString("card_id"));
}
System.out.println("加载数据共"+col.size()+"条");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method initializes this
*
* @return void
*/
public void init() {
this.setSize(600, 300); this.add(getRandDisplay(), null);
this.add(getOutputSpace(), null);
this.add(getStart(), null);
} /**
* This method initializes outputSpace
*
* @return javax.swing.JTextArea
*/
public JTextArea getOutputSpace() {
if (outputSpace == null) {
outputSpace = new JTextArea();
// outputSpace.setText("test");
outputSpace.setBackground(Color.lightGray);
outputSpace.setLineWrap(true);
outputSpace.setPreferredSize(new Dimension(600, 200));
// outputSpace.setAutoscrolls(true);
}
return outputSpace;
} /**
* This method initializes Start
*
* @return javax.swing.JButton
*/
private JButton getStart() {
if (Start == null) {
Start = new JButton();
Start.setText("Start");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) { //System.out.println("actionPerformed()" + col.size());
//rander.start();
//rander.yield();
timer.scheduleAtFixedRate(rander, 1000,1000); }
});
}
return Start;
}
/**
* This method initializes randDisplay
*
* @return javax.swing.JTextField
*/
public JTextField getRandDisplay() {
if (randDisplay == null) {
randDisplay = new JTextField();
randDisplay.setPreferredSize(new Dimension(200, 22));
randDisplay.setBackground(Color.yellow);
}
return randDisplay;
}}
==================================
import java.util.TimerTask;
import java.util.Random;public class Rander extends TimerTask {
private Test thetest;
public Rander(Test t) {
thetest=t;
} public void run() {
// TODO Auto-generated method stub
Random r =new Random ();
//System.out.println(""+r.nextInt());
thetest.getRandDisplay().setText(""+r.nextInt());
}}