在for循环中创建JMenuitem [英] Creating JMenuitem's in a for loop

查看:82
本文介绍了在for循环中创建JMenuitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,我正在尝试这样做:

Greeting, I'm trying to do this:

public float a=0;

for(a=1 ; a<100;a++){
        String fuent="font"+String.valueOf((int)a);
        JMenuItem fuent=new JMenuItem(String.valueOf(a));
        fuent.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){texto.setFont(texto.getFont().deriveFont(a)); current=a;}});
        tamano.add(fuent);
        }

但是会引发以下错误:

cambiar.java:71: error: variable fuent is already defined in constructor cambiar()
        JMenuItem fuent=new JMenuItem(String.valueOf(a));
                  ^

cambiar.java:72: error: cannot find symbol
        fuent.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){texto.setFont(texto.getFont().deriveFont(a)); current=a;}});
             ^

symbol:   method addActionListener(<anonymous ActionListener>)
  location: variable fuent of type String
2 errors
[Finished in 0.5s with exit code 1]

我正在尝试这样做:

JMenuItem (String)fuent=new JMenuItem(String.valueOf(a));
 JMenuItem System.out.println(fuent)=new JMenuItem(String.valueOf(a));

,但均无效。

- -EDIT ----
我想有些人对我想要的东西感到困惑:

---EDIT---- I think some are confuse about what I want:

String fuent="font"+String.valueOf((int)a);
        JMenuItem fuent=new JMenuItem(String.valueOf(a));//(Here sould go the value of the String, Example "font1")
        fuent.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){texto.setFont(texto.getFont().deriveFont(a)); current=a;}});
        tamano.add(fuent); //(Same Here) 


推荐答案

您定义了两个不同的变量与他同名

You defined two different variables with he same name

String fuent ="font"+String.valueOf((int)a);
JMenuItem fuent =new JMenuItem(String.valueOf(a));

尝试重命名一个或两个,例如

Try renaming one or both, for example

String strFuent="font"+String.valueOf((int)a);
JMenuItem miFuent=new JMenuItem(String.valueOf(a));

更新示例

JMenuItem fuent=new JMenuItem("font"+String.valueOf((int)a));

将解决您的问题

在OP编辑后更新

这仍然行不通...

String fuent="font"+String.valueOf((int)a); // You have defined fuent as a String
// Here you are trying to define fuent AGAIN as a JMenuItem
// You CAN NOT DO THIS...
// Change one of the variable names
JMenuItem fuent=new JMenuItem(String.valueOf(a));//(Here sould go the value of the String, Example "font1")
fuent.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){texto.setFont(texto.getFont().deriveFont(a)); current=a;}});
tamano.add(fuent); //(Same Here) 

现在可以使用...

String fuent1="font"+String.valueOf((int)a); // You have defined fuent as a String
JMenuItem fuent=new JMenuItem(fuent1);
fuent.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){
        texto.setFont(texto.getFont().deriveFont(a)); current=a;
    }
});
tamano.add(fuent); //(Same Here) 

这篇关于在for循环中创建JMenuitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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