Java Swing - mouseMoved 事件触发缓慢

标签 java swing mouse

目前我在 Java - Swing 中遇到 mouseMoved 事件的问题。简而言之,我有一个 JPanel 并已将 MouseMotionListener 附加到它,以便动态隐藏或显示 JscrollPane:

myPanel.addMouseMotionListener(new MousePresenter());

我有自己的实现 MouseMotionListener 接口(interface)的类:

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();
   }

  }

} 

问题是 mouseMoved 事件的触发频率不够高。使用 MouseMotionListener 时是否有解决此问题的相关解决方案?

感谢您的宝贵时间。

最佳答案

以下似乎对我来说工作得很好。请注意,事件的处理速度相当快:

  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 );
      }
    } );
  }

您的 hideScrollBar 方法可能不是这种情况

关于Java Swing - mouseMoved 事件触发缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618594/

相关文章:

java - 我安装了 Java 7 但 Eclipse 一直说 1.6 不适合这个产品

java - 在 Java 中创建一个以数组作为参数的对象

java - 如何将我的切换按钮值转换为 int

c# - 将 key 发送到 C# 中的特定句柄?

java - 关联列表,Arraylist Java

java - 如何对某个区域内的非GUI对象实现MouseListener?

Java:是否可以(在运行时)获取 future 方法调用的源文件名和行号?

haskell - 如何在 Haskell Gloss 中隐藏鼠标光标

c++ - 从鼠标坐标到 3d 的点-三角形相交?

java - 如何检索 JSONObject 子字段?