Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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 动态创建jbutton_Java_Swing_Dynamic_Jbutton - Fatal编程技术网

Java 动态创建jbutton

Java 动态创建jbutton,java,swing,dynamic,jbutton,Java,Swing,Dynamic,Jbutton,你好,我已经准备好了 private JButton btnFoo, btnBar; 我需要为每个按钮获取以下信息 btnFoo = new JButton("Foo"); btnFoo.addActionListener(this); add(btnFoo); 在Java中,是否可以为我声明的每个按钮动态创建该按钮? 因为当我有5个按钮时,我不需要3x5=15行代码,而只需要几行动态创建的按钮。这就是数组和循环的作用所在。使用按钮数组并对其进行迭代。 但是如

你好,我已经准备好了

private JButton btnFoo, btnBar;
我需要为每个按钮获取以下信息

        btnFoo = new JButton("Foo");
    btnFoo.addActionListener(this);
    add(btnFoo);
在Java中,是否可以为我声明的每个按钮动态创建该按钮?
因为当我有5个按钮时,我不需要3x5=15行代码,而只需要几行动态创建的按钮。

这就是数组和循环的作用所在。使用按钮数组并对其进行迭代。 但是如果您担心标识符,那么使用
HashMap
可能是一个好方法

Map<String, JButton> buttons = new HashMap<String, JButton>();
map.put("fooButton", new JButton());
...
Map按钮=新建HashMap();
put(“fooButton”,newjbutton());
...

通过迭代条目集(仅几行代码),为按钮设置
ActionListener

这就是数组和循环的作用。使用按钮数组并对其进行迭代。 但是如果您担心标识符,那么使用
HashMap
可能是一个好方法

Map<String, JButton> buttons = new HashMap<String, JButton>();
map.put("fooButton", new JButton());
...
Map按钮=新建HashMap();
put(“fooButton”,newjbutton());
...

通过迭代条目集(仅几行代码),为按钮设置
ActionListener

编写一个小循环并将按钮存储在数组中:

private JButton buttons[] = new JButton[5];

String names[] = {"Foo", "Bar", "Baz", "Fob", "Bao"};
for (int i = 0; i < buttons.length; ++i)
{
    JButton btn = new JButton(names[i]);
    btn.addActionListener(this);
    add(btn);
    buttons[i] = btn;
}
private JButton button[]=新JButton[5];
字符串名[]={“Foo”、“Bar”、“Baz”、“Fob”、“Bao”};
用于(int i=0;i
编写一个小循环并将按钮存储在一个数组中:

private JButton buttons[] = new JButton[5];

String names[] = {"Foo", "Bar", "Baz", "Fob", "Bao"};
for (int i = 0; i < buttons.length; ++i)
{
    JButton btn = new JButton(names[i]);
    btn.addActionListener(this);
    add(btn);
    buttons[i] = btn;
}
private JButton button[]=新JButton[5];
字符串名[]={“Foo”、“Bar”、“Baz”、“Fob”、“Bao”};
用于(int i=0;i
boton1=newjbutton();
boton1.setText(“结石”);
添加(boton1);
boton1.setActionCommand(“Calcular”);
boton1.addActionListener(此);
boton1.setEnabled(真);
字符串nB2=“boton”;
对于(intj=2;j
boton1=newjbutton();
boton1.setText(“结石”);
添加(boton1);
boton1.setActionCommand(“Calcular”);
boton1.addActionListener(此);
boton1.setEnabled(真);
字符串nB2=“boton”;

对于(int j=2;jbuttons[i]=btn;do到底是什么?@Thorvason:它将指向新创建的JButton对象的指针存储在名为
buttons
指针的数组中(在第一行创建)谢谢你,bc我目前正在我的项目中使用它,我在项目中基于输入xml文件创建JButton,这是其中的一个要点buttons[I]=btn;do到底是什么?@Thorvason:它将指向新创建的JButton对象的指针存储在名为
JButton
按钮的
指针数组中(这是在第一行创建的)。谢谢你,bc我目前正在我的项目中使用它,我在项目中基于输入xml文件创建Jbuttons,这是本文的重点之一