Creating JMenuitem's in a for loop

我是研究僧i 提交于 2019-12-24 03:35:23

问题


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);
        }

But it throws these errors:

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]

I've trying to do this:

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

but none works.

---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) 

回答1:


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));

UPDATED EXAMPLE

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

Will solve your problems

UPDATED after OP Edits

This will still not work...

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) 

This will now work...

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) 



回答2:


You should learn the basics of Java, as your code has major basic issues (the float a can't be defined public there, unless it was really somewhere else and you just put it there to show us it). You cannot define the same name for a variable twice; call one fuentMenu and one fuentString or whatever.



来源:https://stackoverflow.com/questions/12223196/creating-jmenuitems-in-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!