如题:现在有一个按钮,当的点击这个按钮并且不释放的时候进行数据累加,鼠标释放时,数据累加就停止。
功能和滚动条的一样,也就是按住,滚动条的向左或向右箭头时,滚动条就是自动向左或向右移,
这个功能如何实现?如果解决,另有加分。。

解决方案 »

  1.   

    mousePressed(MouseEvent e) 这个是鼠标按下时的监听mouseReleased(MouseEvent e) 这个是鼠标释放时的监听在mousePressed里面累加数据,在mouseReleased停止累加
      

  2.   

    如果你是需要不停的累加的话,那么在mousePressed里面添加一个Timer线程,让数据在Timer线程里面累加,然后在mouseReleased里面停止Timer线程就好了
      

  3.   


    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    /**
     * 
     * @author close
     * @version 1.0
     */
    public class SwingDemo extends JFrame {
    private JButton jb;
    private JTextField jt;
    private long count = 0;
    private Timer time = null;
    public SwingDemo() {
    this.setSize(200, 200);
    this.setLocation(300, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    jb = new JButton("add count");
    jt = new JTextField("count");
    jb.addMouseListener(new MouseListener() {
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    time = new Timer(10, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jt.setText(String.valueOf(++count));
    } });
    time.start();
    }
    public void mouseReleased(MouseEvent e) {
    time.stop();
    }
    });
    this.getContentPane().add(jb);
    this.getContentPane().add(jt);
    this.setVisible(true);
    }
    public static void main(String[] args) {
    new SwingDemo();
    }}
      

  4.   


    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Timer;
    import java.util.TimerTask;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;public class Test extends JFrame implements MouseListener{
    private static final long serialVersionUID = 1L;

    private JButton textButton = null;
    private JLabel textLabel = null;
    private Timer timer = null;

    private long count = 0; public Test(){
    super();
    initialize();
    }

    private void initialize(){
    setTitle("数据累加");
    setSize(300,200);
    setPreferredSize(new Dimension(300,200));
    setResizable(false);
    this.setLayout(new BorderLayout());
    add(getTextButton(),BorderLayout.WEST);
    add(getTextLabel(),BorderLayout.EAST);
    }

    private JButton getTextButton(){
    if(textButton == null)
    {
    textButton = new JButton("控制按钮");
    textButton.setPreferredSize(new Dimension(120,30));
    textButton.setVisible(true);
    textButton.addMouseListener(this);
    }
    return textButton;
    }

    private JLabel getTextLabel(){
    if(textLabel == null)
    {
    textLabel = new JLabel("0");
    textLabel.setPreferredSize(new Dimension(30,30));
    textLabel.setVisible(true);
    }
    return textLabel;
    } @Override
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

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

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

    } @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    ProTimestart();
    } @Override
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    stop();
    }

    private void ProTimestart()
    {
    if(timer != null)
    {
    return;
    }
    timer = new Timer();
    timer.schedule(new TimerTask()
    {
    public void run()

    textLabel.setText(Long.toString(++count));
    }
    } , 0,10
    );
    }

    private void stop(){
    if(timer != null)
    {
    timer.cancel();
    timer = null;
    }
    }

    public static void main(String[] args){
    Test test = new Test();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setVisible(true);
    }
    }
    随便写了下,不知道达不达到你的要求,你看看