gpt4 book ai didi

java - JButton 添加对象

转载 作者:行者123 更新时间:2023-12-01 17:23:36 25 4
gpt4 key购买 nike

有没有办法向 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);
}

}

因此,每个按钮都引用一个对象或保存一个对象,因此如果单击该按钮,我就可以使用它保存的对象。

最佳答案

如果您需要这种行为,OO 的奇迹允许您添加它:

public class JReferencingButton<T> extends JButton 
{
private T value;

public T getValue()
{
return this.value;
}

public void setValue(T value)
{
this.value = value;
}
}

关于java - JButton 添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com