3

I search through this forum but I can't find a good explanation on this topic. Is there any different from the below statements? Which way is the better way to trigger this event?

if (evt.getButton() == 3) 

Vs.

if (SwingUtilities.isRightMouseButton(evt))
4

1 回答 1

7

SwingUtilities.isRightMouseButton(evt)

这使用位操作数进行比较:

(evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)

而其他,只需与一个可以改变的幻数进行比较,我宁愿将第一个比较更改为:

if(evt.getButton() == java.awt.event.MouseEvent.BUTTON3)

SwingUtilities是 jdk 附带的 javax 的一部分,因此我更愿意将该责任委托给该实用程序(帮助程序)类,因此在这种情况下,SwingUtilities获胜。

于 2014-01-08T08:39:18.173 回答