如何实现3状态切换按钮?我正在使用eclipse和windowBuilder,我正在编写一个必须有三态按钮的程序。我使用JToggleButtons和他们的两个状态为两个bu无法实现第三个状态。我该怎么办?
P.S。我想要三个图标状态。
答案 0 :(得分:1)
也许您可以查看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 {