Is it possible to associate some custom (of type Object
) data with Swing/AWT control?
I am creating multiple toggle buttons on a panel to select one of multiple objects, and want to remember, which object each button selects.
在 Swing 中,您可以将 putClientProperty 用于 JComponent。也许这可以帮助你: http ://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#putClientProperty%28java.lang.Object,%20java.lang.Object%29
祝你好运!
我经常处理这种情况的方法是将 aHashMap<JButton, Object>
作为类字段存储,并在按下按钮时更新它。对于切换按钮,打开按钮可以将按钮/对象对添加到地图中,关闭按钮可以将其删除。或者,如果您只想跟踪已选择的对象,请存储ArrayList<Object>
并以类似方式更新它。