在按钮中插入字符数组

发布于 2024-12-05 17:53:30 字数 358 浏览 1 评论 0原文

我试图将字母表放在不同的按钮中,但我无法使其工作。我的代码如下所示:

char[] Letter = {'a','b','c','d','e','f','g','h'
                    ,'i','j','k','l','m','n','o','p','q'
                    ,'r','s','t','u','v','w','x','y','z'};
Button[] But;
for (int i = 0; i <= 26; ++i) {
    But = new Button(Letter[i]);
    this.add(But[i], BorderLayout.SOUTH);
}

I'm trying to put the alphabet in different buttons, but I cant make it work. My code looks like this:

char[] Letter = {'a','b','c','d','e','f','g','h'
                    ,'i','j','k','l','m','n','o','p','q'
                    ,'r','s','t','u','v','w','x','y','z'};
Button[] But;
for (int i = 0; i <= 26; ++i) {
    But = new Button(Letter[i]);
    this.add(But[i], BorderLayout.SOUTH);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窝囊感情。 2024-12-12 17:53:30

这应该对你有用。 Java 中的约定是以小写字母开头命名变量。

char[] letters = {'a','b','c','d','e','f','g','h'
        ,'i','j','k','l','m','n','o','p','q'
        ,'r','s','t','u','v','w','x','y','z'};
Button[] buttons = new Button[26];

for(int i = 0;i< 26;++i){
    buttons[i] = new Button(Character.toString(letters[i])); //need to convert char to String first
    this.add(buttons[i],BorderLayout.SOUTH);
}

This should work for you. Its convention in Java to name variables starting with a lower case letter.

char[] letters = {'a','b','c','d','e','f','g','h'
        ,'i','j','k','l','m','n','o','p','q'
        ,'r','s','t','u','v','w','x','y','z'};
Button[] buttons = new Button[26];

for(int i = 0;i< 26;++i){
    buttons[i] = new Button(Character.toString(letters[i])); //need to convert char to String first
    this.add(buttons[i],BorderLayout.SOUTH);
}
二手情话 2024-12-12 17:53:30
char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(char c : letters) add(new JButton(new String(c)));
char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(char c : letters) add(new JButton(new String(c)));
GRAY°灰色天空 2024-12-12 17:53:30

你可能想要这个

But[i] = new Button(Letter[i]);

而不是这个

But = new Button(Letter[i]);

You probably wanted this

But[i] = new Button(Letter[i]);

instead of this

But = new Button(Letter[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文