简体   繁体   English

在Java swing中操纵时间

[英]manipulating time in java swing

I was trying to manipulate an amateur animation using Swing in Java . 我试图使用Java Swing操纵业余动画。 The goal was to start an animation if I hover over a certain button . 目标是如果我将鼠标悬停在某个button ,则开始制作动画。 That was easy. 那很简单。 But the problem is, I want to manipulate the speed of the timer of the animation according to the coordinates of the button. 但是问题是,我想根据按钮的坐标来控制动画计时器的速度。

For example, the more right I go on the button the more speedy the animation gets (delay time decreases) and vice-versa. 例如,我在buttonbutton的越多,动画获得的速度就越快(延迟时间减少),反之亦然。 The code I wrote works if I move the arrow out of the button then enter again. 如果我将箭头移出按钮,然后再次输入,我编写的代码将起作用。 Then the delay changes according to co-ordinates. 然后,延迟根据坐标而变化。 But not if I move the arrow within the region of the button. 但是如果我在按钮区域内移动箭头,则不会。 I used mouseEntered here. 我在这里使用mouseEntered That may cause cause the problem. 那可能会引起问题。 But the mouseMove function seems not to work too. 但是mouseMove函数似乎也不起作用。

Here's my code: 这是我的代码:

private void jButton1MouseEntered(java.awt.event.MouseEvent evt)
{
  // TODO add your handling code here:
  PointerInfo inf = MouseInfo.getPointerInfo();
  Point a = inf.getLocation();
  double x = a.getX();
  final int n = (int) (x - 161);
  //String str=""+x;
  //Output.setText(str);
  ActionListener taskPerformer = null;
  taskPerformer = new ActionListener()
  {
    @Override
    public void actionPerformed(ActionEvent evt)
    {
      //...Perform a task...
      //if(t>0)timer.setDelay(1000);
      if (t > 0)
      {

        timer.setDelay(500 + 50 * n);
      }

      if (label1 == 0)
      {
        jTextField1.setText(" A");
      }
      else if (label1 == 1)
      {
        jTextField1.setText(" B");
      }
      else if (label1 == 2)
      {
        jTextField1.setText(" C");
      }
      else if (label1 == 3)
      {
        jTextField1.setText(" D");
      }
      else if (label1 == 4)
      {
        jTextField1.setText(" E");
      }
      else if (label1 == 5)
      {
        jTextField1.setText(" F");
      }
      else if (label1 == 6)
      {
        jTextField1.setText(" G");
      }

      if (label1 >= 7)
      {
        label1 = 0;
      }
      else
      {
        label1++;
      }

      t++;

      //System.out.printf("Reading SMTP Info. %d\n",x);

      //System.out.printf("Reading SMTP Info. %d\n",label1t);
      //jLabel3.setText("fsd");
    }

    private Object getContentPane()
    {
      throw new UnsupportedOperationException("Not supported yet.");
    }
  };

  timer = new Timer(500, taskPerformer);
  timer.start();

  //timer.setRepeats(false);
  try
  {
    Thread.sleep(10);
  }
  catch (InterruptedException ex)
  {
    Logger.getLogger(Keyboard_Gui.class.getName()).log(Level.SEVERE, null, ex);
  }
}

But the mouseMove function seems not to work too. 但是mouseMove函数似乎也不起作用。

The mouseMoved event is generated by a MouseMotionListener (not a MouseListener) so you will also need to implement that listener to manipulate the Timer interval. mouseMoved事件由MouseMotionListener (而不是MouseListener)生成,因此您还需要实现该侦听器来操纵Timer间隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM