myecplise 代码老是报出无final的错误

代码是同学那拷的没问题,但是在my上就报这个错误
clipboard.png

clipboard.png

*
//package java_test6;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExRate {

public static void main(String[] args) {
    JFrame f = new JFrame(); //窗口容器
    f.setSize(200, 200);
    JTextArea t1 = new JTextArea(1, 5);//指定行列
    JTextArea t2 = new JTextArea(1, 5);
    JButton b1 = new JButton("test it");
    Label l1, l2;
    l1 = new Label();
    l2 = new Label();
    l1.setText("Please Enter The Number");
    l2.setText("The Result Of The Test");
    Choice c = new Choice();  //这里需final
    c.add("RMB");
    c.add("Euro");
    c.add("USA$");
    Choice c1 = new Choice();
    c1.add("RMB");
    c1.add("Euro");
    c1.add("USA$");
    f.add(l1);
    f.add(t1);
    f.add(c);
    f.add(l2);
    f.add(t2);
    f.add(c1);
    f.add(b1);
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (c.getSelectedItem() == "RMB" && c1.getSelectedItem() == "USA$") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 0.1506;
                String r = Double.toString(w);
                t2.setText(r);
            } else if (c.getSelectedItem() == "USA$" && c1.getSelectedItem() == "RMB") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 6.6391;
                String r = Double.toString(w);
                t2.setText(r);
            } else if (c.getSelectedItem() == "RMB" && c1.getSelectedItem() == "Euro") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 0.1297;
                String r = Double.toString(w);
                t2.setText(r);
            } else if (c.getSelectedItem() == "Euro" && c1.getSelectedItem() == "RMB") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 7.7107;
                String r = Double.toString(w);
                t2.setText(r);
            } else if (c.getSelectedItem() == "USA$" && c1.getSelectedItem() == "Euro") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 0.8610;
                String r = Double.toString(w);
                t2.setText(r);
            } else if (c.getSelectedItem() == "Euro" && c1.getSelectedItem() == "USA$") {
                String s;
                s = t1.getText();
                int q = 0;
                q = Integer.parseInt(s);
                double w;
                w = q * 1.1614;
                String r = Double.toString(w);
                t2.setText(r);
            }
        }
    });
    f.setLayout(new FlowLayout());
    f.setVisible(true);
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

}*

阅读 1.7k
2 个回答

把完整代码贴出来看看,估计是你在匿名内部类里使用了非final变量...

在作用域里没有找到一个非Final的局部变量,这是这个错的意思,建议去你的getSelectedItem()里面看看或者把你choice这个类贴出来看看

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题