小弟刚刚接触JAVA,请大哥大姐们多多指教,昨天老师让我们做个模拟抽奖的界面,要求号码不停的跳动,说用swing下的Timer,具体也没说怎么用,那位哥哥指点一下,最好给个类子,谢谢了!

解决方案 »

  1.   

    不太清楚..仔细看看API了..不一定非用一种方法..用多线程想想..
      

  2.   

    搞个子类用WHILE循环就可以了class main{
    //存在一个LABEL
    JLabel ex;
    //定义一个子类
      new time().start();
    }
    //子类
    class time extends Thread{
      public void run(){
        while(true){
          ex.setText(显示内容);
          Thread.sleep(100);   //每秒变化10次
        }
      }
    }
      

  3.   

    import javax.swing.*;
    import java.awt.event.*;class TimerTest 
    {
    JFrame mainFrame;
    JPanel mainPanel;
    JLabel label;
    Timer timer;
    static int count = 0;
    public TimerTest() {
    mainFrame = new JFrame (  );
    mainPanel = new JPanel ();
    label = new JLabel ("this is the 0th time the timer executes");
    timer = new Timer( 1000,  new ActionListener(){

    public void actionPerformed( ActionEvent e){
    label.setText( String.format("this is the " + count +"th time the timer executes"));
    count++;
    }
    });
    timer.setInitialDelay(1000);
    timer.start();
    mainPanel.add(label);
    mainFrame.getContentPane().add( mainPanel );
    mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    mainFrame.setSize(300,100);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible( true );
    }
    public static void main(String[] args) 
    {
    SwingUtilities.invokeLater( new Runnable(){
    public void run(){
    new TimerTest();
    }
    });
    }
    }