简体   繁体   English

Java Swing-mouseMoved事件缓慢触发

[英]Java Swing - mouseMoved event is fired slowly

Currently I am experiencing issues with the mouseMoved event in Java - Swing. 目前,我在Java-Swing中遇到mouseMoved事件的问题。 Briefly, I have got a JPanel and I have attached MouseMotionListener to it, in order to hide or show JscrollPane on the fly: 简要地说,我有一个JPanel,并已将MouseMotionListener附加到该JPanel,以便动态隐藏或显示JscrollPane:

myPanel.addMouseMotionListener(new MousePresenter());

I have got my own class that implements MouseMotionListener interface: 我有自己的类,该类实现了MouseMotionListener接口:

public class MousePresenter implements MouseMotionListener { 

  public void mouseMoved(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();

   if (x>20 && x<200) {
    hideScrollBar();
   }
   else {
    showScrollBar();
   }

  }

} 

The issue is that the mouseMoved event is not being fired often enough. 问题在于,mouseMoved事件的触发频率不够高。 Is there any related solution to this issue whilst using MouseMotionListener? 使用MouseMotionListener时是否有与此问题相关的解决方案?

Thank you for your time. 感谢您的时间。

The following seems to work just fine for me. 以下似乎对我来说很好。 Note that the handling of the event is rather fast: 请注意,事件的处理相当快:

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        JPanel content = new JPanel( new BorderLayout() );

        final JLabel mousePosition = new JLabel( "Unknown" );
        content.add( mousePosition, BorderLayout.NORTH );

        content.addMouseMotionListener( new MouseMotionAdapter() {
          @Override
          public void mouseMoved( MouseEvent e ) {
            mousePosition.setText( "X: " + e.getX() + " Y: " + e.getY() );
          }
        } );
        frame.setContentPane( content );
        frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
      }
    } );
  }

That might not be the case for your hideScrollBar method 您的hideScrollBar方法可能不是这种情况

A mouse moved event is inherently slowly since it's fired on every pixel change. 鼠标移动事件本质上是缓慢的,因为每次像素更改都会触发该事件。

The only thing you can do to optimize the whole issue is to optimize what you do inside the callback handler. 优化整个问题的唯一方法是优化回调处理程序内部的操作。 In your case you do have 就你而言

if (something)
  doA();
else
  doB();

This means that in any case you are either trying to show or to hide the scrollbar even when it's already shown or hidden. 这意味着无论如何,即使已显示或隐藏滚动条,您也要尝试显示或隐藏它。 What you can do is: 您可以做的是:

if (scrollBarIsVisible && x>20 && x<200) {
  hideScrollBar();
  scrollBarIsVisible = false;
}
else if (!scrollBarIsVisible) {
  showScrollBar();
  scrollBarIsVisible = true;
}

So that you only modify the visibility of the element (which can be a heavy operation since it may require to relayout things) when switching from inside the bounds to outside and viceversa. 这样,当您从边界内部切换到外部(反之亦然)时,只需修改元素的可见性(这可能是一项繁重的操作,因为可能需要重新布局事物)。 This should lower the computational operations by a lot. 这将大大降低计算运算量。

If you all your code is being executed in the Event Dispatch thread it could be causing problems. 如果您在事件分发线程中执行所有代码,则可能会引起问题。 Have a look at this trail and try to put any code that does a lot of work in a SwingWorker thread. 看一下这条线索,然后尝试将可以完成很多工作的任何代码放入SwingWorker线程中。

Your code is not very well optimized. 您的代码没有很好的优化。 As it is, it will always call either the show or hide Scrollbar methods. 照原样,它将始终调用show或hide Scrollbar方法。 You should probably modify it such as it hides it only if visible and it displays it only if hidden. 您可能应该对其进行修改,例如仅在可见时将其隐藏,而仅在隐藏时将其显示。

Problem solved. 问题解决了。 There was certain performance issue in my app that caused such delays. 我的应用程序中存在某些性能问题,导致了此类延迟。 Thank you for your effort and piece of information and advice you provided. 感谢您的努力以及您提供的信息和建议。

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

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