想做一个applet
在applet里面有一个textfield 还有一个按钮
点击按钮之后textfield每隔1秒随机显示一个数字
再点击按钮就停止显示这样的效果怎么做出来?
我现在是点完按钮生成一个线程刷新textfield里面的数字,可是进到start()里面开始while循环刷新数字之后 那个按钮就不弹起来了 textfield 也没有显示东西,但是console却能够打印出来那些随机数字, 应该是他没从while里面跳出来吧? 我该怎么做呢?谢谢

解决方案 »

  1.   

    //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.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();
    }
    //}
    }}
      

  2.   

    不好意思 用Timer搞出来了,代码贴出来
    ================
    //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());
    }}