问候,我正在尝试这样做:
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));但都不管用。
-编辑-我认为有些人对我想要的东西感到困惑:
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) 发布于 2012-09-01 06:32:43
您定义了两个具有相同名称的不同变量
String fuent ="font"+String.valueOf((int)a);
JMenuItem fuent =new JMenuItem(String.valueOf(a));尝试重命名一个或两个,例如
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) 发布于 2012-09-01 06:32:53
您应该学习Java的基础知识,因为您的代码有主要的基本问题(浮动a不能在那里定义为public,除非它真的在其他地方,您只是将它放在那里向我们展示它)。您不能为一个变量定义两次相同的名称;调用一个fuentMenu和一个fuentString或其他任何名称。
https://stackoverflow.com/questions/12223196
复制相似问题