在 swing 中创建复选框很容易,如下所示:
JCheckBox checkbox = new JCheckBox("hello")
问题是我还想将一些其他值与此复选框相关联
,例如另一个数字,99
但是我找不到 JCheckBox
的任何方法来执行此操作,该怎么做?
最佳答案
选项 3。
你确实需要存储很多值......扩展它......
public class MySuperStoreAlotOfValuesCheckbox extends JCheckBox{
private static final long serialVersionUID = 1L;
private int theInt;
... all the values in the world...
public MySuperStoreAlotOfValuesCheckbox(String text){
super(text);
}
public MySuperStoreAlotOfValuesCheckbox(String text, int theInt){
super(text);
this.theInt = theInt;
}
}
选项 4 正如 @mKorbel 在评论中建议的
checkbox.putClientProperty("myInt",99);
The
clientProperty
dictionary is not intended to support large scale extensions to JComponent nor should be it considered an alternative to subclassing when designing a new component.
关于java - 如何将额外数据存储到 Swing 复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667084/