I built a Swing GUI application, and everything works quite well, except for a little de开发者_开发知识库tail: I have two bottons, and each of them has a mouseclick event attached. The problem is, when i click them, they stay "pressed" : everything else works fine, but it's just odd seeing those two buttons ... maybe i used the wrong event? or i should set them to "depressed" manually ?
"Answers from comments" C.W.-style.
You shouldn't add MouseListeners to JButtons, but instead should use ActionListeners or Action objects. This is the preferred method and is generally simpler if it covers the required behavior.
Make sure to propagate the event (call the default event handlers). The "stays depressed" is because an event is being swallowed and not propagated as it should be.
Is you listener perhaps throwing an exception? Whether this prevents the event getting to the correct other listener depends upon unspecified ordering and one event failing may cause the entire dispatch to fail.
There's gotta be an exception getting thrown that prevents the EDT from completing the button refresh. This is pretty typical.
精彩评论