【问题标题】:Getting the name assigned to a JComponent获取分配给 JComponent 的名称
【发布时间】:2016-07-12 03:27:27
【问题描述】:

我有一个 arrayList,其中的名称数量取决于它的使用方式。我为具有 arrayList 名称的函数之一创建 JRadioButtons,例如:

for(int i = 0; i < m_fixtures.size(); i++){
    panel.add(new JRadioButton(m_fixtures.get(i)));
}

有什么方法可以检查已分配给该 JRadioButton 的名称,因为我想做一些类似的事情

获取选定的 Jradiobuttons“标签”并向该数据添加一些操作,但我不知道如何获取分配给它的“标签”。

【问题讨论】:

  • 你试过调用 getText() 吗?
  • @RubioRic 但按钮没有特定标识符,例如 JRadioButton button = new JRadioButton("text");我不知道如何在没有实例名称的按钮上调用 .getText()。
  • @PigeonMilkStories 也许我的回答有帮助

标签: java swing


【解决方案1】:
   Component[] components = panel.getComponents();

   for (Component singleComponent : components) {
       if (singleComponent instanceof JRadioButton) {
           JRadioButton jrb = (JRadioButton) singleComponent;
           // println or whatever you want
           System.out.println(jrb.getText());
       }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多