Netbeans 中 Swing 组件的变量名 [英] Variable name of Swing component in Netbeans

查看:28
本文介绍了Netbeans 中 Swing 组件的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,目前使用了 200 多个按钮,每个按钮都返回其变量名称的字符串.有没有办法做到这一点?为每一个设置 name 属性会非常耗时.

I have an application that is currently using over 200 buttons, each of which returns the String of their variable name. Is there any way to do this? Setting the name property for each of these would be far too time consuming.

推荐答案

使用按钮集合:

ActionListener theActionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(((JButton) e.getSource()).getName());
    }
};

List<JButton> buttons = new ArrayList<JButton>();
for (int i = 0; i < 200; i++) {
    JButton button = new JButton("Button " + (i + 1));
    button.setName("Button " + (i + 1));
    button.addActionListener(theActionListener);
    buttons.add(button);
}

这篇关于Netbeans 中 Swing 组件的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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