如何重命名一组JButton? [英] How to rename set of JButtons?

查看:78
本文介绍了如何重命名一组JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有25个jButton,我想从循环中更改其文本.这是我的1个按钮的代码.

I have 25 jButtons and i want to change their texts from a loop. Here is my code for 1 button..

void changeText(){
            jButton1.setText(jButton1.getText().toUpperCase());

    }

我想对所有其他按钮执行相同操作,而不必为每个按钮编写方法.

I want to do the same for all other buttons without writing a method for each.

是否可以使用类似的东西?

Is it possible to use something like this?

void changeText(){
        for(int i=0;i<25;i++){
            String x = "jButton"+i;
            x.setText(x.getText().toUpperCase());
        }
    }

这肯定行不通.请给我建议一种方法.

Surely this wont work. Please suggest me a method.

推荐答案

您可以通过将按钮添加到集合中来实现.

You can do this by adding the buttons to a collection.

类似这样的东西:

// initialization of jbuttons:
List<JButton> buttons = new ArrayList<JButton>();
JButton jbutton1 = new JButton();
// .. set properties
buttons.add(jbutton1);

// add more jbuttons to the list

稍后您可以遍历按钮列表:

Later you can iterate over the list of buttons:

for (JButton button : buttons) {
  button.setText(button.getText().toUpperCase());
}

这篇关于如何重命名一组JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆