当 JButton 的子类启用/禁用时,我可以重写什么方法来触发事件
我创建了 JButton 的子类。我能够使用以下代码覆盖鼠标事件:
@Override
protected void processMouseEvent(MouseEvent e) {
super.processMouseEvent(e);
// My event code...
}
我想知道应该覆盖什么才能在启用或禁用对象时发生事件。
我对 JButton 进行子类化主要是出于美观的原因,这样我就可以准确地设置按钮的外观。我希望能够在按钮被禁用时将其变灰。如果您认为我应该采用不同的方式,请告诉我。
I've created a sub-class of JButton. I was able to override mouse events with the following code:
@Override
protected void processMouseEvent(MouseEvent e) {
super.processMouseEvent(e);
// My event code...
}
I was wondering what I should override to get events to happen when the object is enabled or diasbled.
My subclassing of JButton was mostly for cosmetic reasons, just so I can set exactly what the buttons look like. I want to be able to gray out the buttons when they become disabled. If you think I should be doing this a different way, please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要覆盖任何内容。启用或禁用该按钮将触发一个属性名称为“enabled”的 propertyChange 事件。
You don't need to override anything. Enabling or disabling the button will trigger a propertyChange event with "enabled" as the property name.
重写enable()、disable() 和setEnabled() 吗?
顺便说一句,重写 processMouseEvent() 听起来......肮脏......你应该添加一个 MouseListener 来代替
override enable(), disable(), and setEnabled() ?
btw, overriding processMouseEvent() sounds... dirtyish... you should probably add a MouseListener instead