【问题标题】:Java, set ID for JButtonJava,为 JButton 设置 ID
【发布时间】:2012-04-21 15:31:10
【问题描述】:

无论如何要为JButton 设置一个ID。我在 Android 中已经习惯了。

我正在寻找类似以下的内容:

newButton.setId(objectcounter);

【问题讨论】:

  • “我在 Android 中已经习惯了。” 在 Android 中的处理方式可能与在 J2SE 中不同。习惯于像语言通常那样做。在罗马时..

标签: java swing jbutton


【解决方案1】:

您可以使用一个属性名称:

newButton.setName(String.valueOf(objectCounter))

或者,您可以使用 clientProperties 来存储任意值:

newButton.putClientProperty("id", Integer.valueOf(objectCounter))

要从客户端属性映射中获取值,您需要这样的东西。

Object property = newButton.getClientProperty("id");
if (property instanceof Integer) {
   int objectCounter = ((Integer)property);
   // do stuff
}

【讨论】:

  • 有没有办法将它与 drop 功能一起使用。因为我需要识别不同种类的水滴
  • 这些可用于需要StringMap.Entry<Object, Object> 的任何内容。
  • @user1321361 我添加了一个如何获取值的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2020-04-19
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多