日期:2014-05-20  浏览次数:20766 次

JButton的Text问题
我想在JButton上实现一个功能,最初JButton上显示“点击”,点击JButton后会出现倒计时功能,最后JButton消失。
即点击后出现10,过一秒后出现9,。。。。知道最后等于1,然后消失。
在监听器里我是这样写的:
public void mouseClicked(MouseEvent evt) {  
  while(num > 0){  
  jButton1.setText(num+"");
  num--;
  try {
  Thread.sleep(1000);
  } catch (InterruptedException ex) {
  Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
  }
  }
}
这段代码是直接显示最后那个数字的,有什么办法让数字逐一显示?

------解决方案--------------------
不要堵塞当前线程Thread.sleep(1000); 要么新开一个线程,要么用timer控制
have a try
Java code
public void mouseClicked(MouseEvent evt) { 
  Object source = e.getSource();
  if (source == jButton1 && jButton1.getEnabled()) {  
      jButton1.setEnabled(false);
      final javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener(){
          int num = 10;
          public void actionPerformed(ActionEvent e) {
              jButton1.setText(num+"");
              num--;
              if (num == 0) {
                  t.stop();
                  jButton1.setEnabled(true);
              }
          }
      });
      t.start();
  }
}

------解决方案--------------------
对于SWING当中的定时器,JDK提供了javax.swing.Timer这个类,下面贴一个完整的Demo,你可以借鉴一下
Java code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * javax.swing.Timer的一个实例
 * @author ioe_gaoyong
 *
 */
public class NumberDemo extends JFrame{
    private int number=10;//初始值为10
    
    private JButton numBtn;
    
    private Timer timer;
    
    private class NumListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {
            // TODO Auto-generated method stub
             numBtn.setText(number+"");
             number--;
             if (number == 0) {
                 numBtn.setText("stop");
                 timer.stop();
                 numBtn.setEnabled(true);
             }
        }
        
    }
    
    public NumberDemo(String title){
        super(title);
        numBtn=new JButton("Click");
        numBtn.addMouseListener(new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                Object source = e.getSource();
                  if (source == numBtn && numBtn.isEnabled()) {  
                      numBtn.setEnabled(false);
                      timer=new Timer(1000,new NumListener());
                      timer.start();
                  }

            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub
                
            }
            
        });
        this.add(numBtn);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args){
        new NumberDemo("NumberDemo");
    }
}

------解决方案--------------------

顶楼上,用Timer