目前我遇到了Java中的mouseMoved事件问题 – Swing.简而言之,我有一个JPanel并且我已经附加了MouseMotionListener,以便隐藏或显示JscrollPane:
myPanel.addMouseMotionListener(new MousePresenter());
我有自己的类实现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();
}
}
}
问题是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方法可能不是这种情况