如何实现三态切换按钮?我正在使用 eclipse 和 windowBuilder,并且正在编写一个必须具有 3 态按钮的程序。我使用了 JToggleButtons 和它们的两个状态,但不能实现第三个状态。我能做什么?
附言我想要三个图标状态。
最佳答案
也许你可以看看com.jidesoft.swing.TristateCheckBox
使用的代码:
参见 TristateCheckBox.java
.
它确实继承自 JToggleButton
并具有三种不同的图标状态。
/**
* Maintenance tip - There were some tricks to getting this code
* working:
* <p/>
* 1. You have to overwite addMouseListener() to do nothing
* 2. You have to add a mouse event on mousePressed by calling
* super.addMouseListener()
* 3. You have to replace the UIActionMap for the keyboard event
* "pressed" with your own one.
* 4. You have to remove the UIActionMap for the keyboard event
* "released".
* 5. You have to grab focus when the next state is entered,
* otherwise clicking on the component won't get the focus.
* 6. You have to make a TristateDecorator as a button model that
* wraps the original button model and does state management.
*
* @author Dr. Heinz M. Kabutz
*/
public class TristateCheckBox extends JCheckBox {
关于java - 使用 JToggleButton 在三种状态之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8115601/