Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:如何使用方法的name参数创建按钮?_Java_Swing_Methods_Jbutton - Fatal编程技术网

JAVA:如何使用方法的name参数创建按钮?

JAVA:如何使用方法的name参数创建按钮?,java,swing,methods,jbutton,Java,Swing,Methods,Jbutton,我想写一个方法,允许创建不同名称的JButton 我希望我能发送一个参数名和一个按钮标题 AddButtonMethod("name", "title"); 方法: private void AddButtonMethod(String name, String title){ JButton name = new JButton(title); name.addActionListener(ecouteurBouton);

我想写一个方法,允许创建不同名称的JButton

我希望我能发送一个参数名和一个按钮标题

AddButtonMethod("name", "title");
方法:

private void AddButtonMethod(String name, String title){


            JButton name = new JButton(title);

            name.addActionListener(ecouteurBouton);

            add(name);

    }
我想要新按钮作为收到patranetre“name”的名称


谢谢你

请理解,变量名的重要性远远低于你可能意识到的,甚至在编译代码后都不存在。更重要和关键的是变量引用——在程序中的特定点访问特定对象的能力,通过使用诸如HashMap之类的映射,字符串可以轻松地引用对象。比如说

// in the variable declaration section of the class
private Map<String, JButton> buttonMap = new HashMap<>();

// your method should be named addButton, not AddButton
private void addButton(String name, String title){
   JButton button = new JButton(title);
   button.addActionListener(ecouteurBouton);
   buttonMap.put(name, button);
   add(button);
}
//在类的变量声明部分
私有映射按钮Map=newhashmap();
//您的方法应命名为addButton,而不是addButton
私有void addButton(字符串名称、字符串标题){
JButton按钮=新JButton(标题);
addActionListener(ecouteurBouton);
按钮映射放置(名称、按钮);
添加(按钮);
}