当按钮按下时,JButton背景和前景颜色滑动 [英] JButton background and foreground color swipe when button pressed
问题描述
任何一个可以帮助实现下面的东西?
Could any one help to implement below thing?
我有自定义按钮,我已经重载了paintComponent和plainText方法给我自己的样子。重写的主要重点是给我自己的风格的按钮。
I have custom button which i have overloaded the paintComponent and plainText method to give my own look. the main focus of overridden is to give my own style for buttons.
所以这里的要求是原来按钮背景是白色的,前景是黑色的,当用户按下按钮
so here the requirement is originally button background is white and foreground is black and when user press the button the color should revert like background as black and foreground as white.
如何实现这个?
推荐答案
您可以提供自己的 UI实现并将其附加到正常的组件实例,而不是对Swing组件进行子类化以修改它们的外观和感觉。
Instead of subclassing Swing components to modify how they «look and feel», you can provide your own UI implementation and attach it to a normal component instance.
查看以下方法:
- 'JComponent.setUI(ComponentUI ui)' :设置UI策略/装饰的方法
- ButtonUI :用于呈现按钮的UI
- BasicButtonUI :基本实现你可以轻松扩展,覆盖
paintButtonPressed
或其他paint
- 方法来创建自己的按钮外观。
- 'JComponent.setUI(ComponentUI ui)': method to set the UI strategy/decoration
- ButtonUI: the UI used for rendering buttons
- BasicButtonUI: basic implementation you can extend easily, overwrite
paintButtonPressed
or the otherpaint
-methods to create your own button look.
这篇关于当按钮按下时,JButton背景和前景颜色滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!