使用JToggleButton在三个状态之间切换

时间:2011-11-13 23:21:15

标签: java eclipse togglebutton windowbuilder

如何实现3状态切换按钮?我正在使用eclipse和windowBuilder,我正在编写一个必须有三态按钮的程序。我使用JToggleButtons和他们的两个状态为两个bu无法实现第三个状态。我该怎么办?

P.S。我想要三个图标状态。

1 个答案:

答案 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 {