Ced*_*ric 0 java swing awt jbutton
有没有办法将对象添加到JButton?我知道我几年前在C#中做过它,但是它是用Java工作的吗?
public Shop(Item[] buyables) {
ArrayList<JButton> buttons = new ArrayList<JButton>();
for(int i = 0; i < buyables.length; i++) {
Item item = buyables[i];
JButton button = new JButton();
button.setText(item.getName());
// button.addExtra(item);
buttons.add(button);
}
}
Run Code Online (Sandbox Code Playgroud)
所以每个按钮都引用一个对象或者保存一个对象,所以如果单击按钮,我就可以使用它所拥有的对象.
如果您需要这种行为,OO的奇迹允许您只需添加它:
public class JReferencingButton<T> extends JButton
{
private T value;
public T getValue()
{
return this.value;
}
public void setValue(T value)
{
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)