Bla*_*uiz 2 java events swing actionlistener jmenuitem
几个小时前我问了这个问题,但我想我没有很好地解释自己.这是我的代码:
for (a = 1; a < 14; a++) {
JMenuItem "jmenu"+a = new JMenuItem(String.valueOf(a));
"jmenu"+a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rrr[a] = a;
texto.setFont(texto.getFont().deriveFont((float) a));
current = a;
}
});
tamano.add("jmenu"+a);
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是JMenuItem
用这些名称创建几个:
jmenu1
jmenu2
jmenu3
jmenu4
etc...
Run Code Online (Sandbox Code Playgroud)
- -编辑 - -
我想要的是每个人JMenuitem
都有不同的名字:
JMenuItem "jmenu"+a //with this I can't create the JMenuItem; it's not permitted
= new JMenuItem(); //I dont care about this
Run Code Online (Sandbox Code Playgroud)
您无法以编程方式命名变量.如果你想要14个不同的组件,那么创建一个数组或List来保存这些组件,然后在循环中创建这些组件并将它们添加到你的数组/列表中.如果你想要第n个组件,你可以使用components [n]或list.get(n)来获取它.