2

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) 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Esteru
  • 177
  • 1
  • 7

2 Answers2

2

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) 
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • The float is public outside the class, the name I know, the problem is that want to name the JMenuItem as the value of the String "Fuent" – Esteru Aug 31 '12 at 22:37
  • The float has nothing to do with it. You CAN NOT HAVE TWO VARIABLES WITH THE SAME NAME OR LABEL. The only thing you can do is change the variable names. The text you supply to the menu item is irrelevent – MadProgrammer Aug 31 '12 at 22:55
  • Did you read the edit that I make?... Im trying yo make the JmenuItem have the value of the String... Example: String hi="Button1"; JMenuItem "Button1"(The value of the String).... – Esteru Aug 31 '12 at 23:19
  • Did you read the answer. The text you are passing is irrelevent to your problem. You have two variables of different types named the same. Change one of the variable names and it will work just fine – MadProgrammer Aug 31 '12 at 23:21
  • If i change the Variable it works... But it will be passing the same JMenuItem to everything... What i mean is that they all will got the last value that "a" has... in this case 99... I will got 99 JmenuItem with the value 99... – Esteru Aug 31 '12 at 23:31
  • I'm sorry to be so insistent...xD... But cheking at what you edit, is not what i need... JMenuItem fuenta(Here, the jMenuItem sould be the value of the String, that way, will not rewrite every ActionListener that i make)=new JMenuItem(String.valueOf(a)); Anyways, Thanks in advance. – Esteru Aug 31 '12 at 23:48
  • Fine remove the line `String fuent="font"+String.valueOf((int)a);` and you menu items will read "1" through "99" – MadProgrammer Sep 01 '12 at 00:21
1

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.

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • The float is public outside the class, the name I know, the problem is that want to name the JMenuItem as the value of the String "Fuent" – Esteru Aug 31 '12 at 22:36