动作侦听器出错,不确定是什么问题

时间:2012-08-03 01:05:56

标签: java

有什么想法吗?第66行是一个花括号,你会在下面看到它,我用//下面显示它。 在eclipse中,它不断出现错误,并且如何摆脱它。有任何想法吗?谢谢! 它是在动作监听器之后写的,我忘了关闭它吗?

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

public class operation extends JFrame{
    private JTextField monday;
    private JTextField tuesday;
    private JTextField wednesday;
    private JTextField thursday;
    private JTextField friday;
    private JTextField saturday;
    private JTextField sunday;
    private JButton finalize;

    public operation(){
        super("Weekly Total Calculator");
        setLayout(new FlowLayout());

        monday = new JTextField("How much you spent Monday", 20);
        monday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(monday);

        tuesday = new JTextField("How much you spent Tuesday", 20);
        tuesday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(tuesday);

        wednesday = new JTextField("How much you spent Wednesday", 20);
        wednesday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(wednesday);

        thursday = new JTextField("How much you spent Thursday", 20);
        thursday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(thursday);

        friday = new JTextField("How much you spent Friday", 20);
        friday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(friday);

        saturday = new JTextField("How much you spent Saturday", 20);
        saturday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(saturday);

        sunday = new JTextField("How much you spent Sunday", 20);
        sunday.setFont(new Font("Ariel", Font.PLAIN, 14));
        add(sunday);

        finalize = new JButton("Calculate!");
        add(finalize);

        finalize.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent ae){

                    double mon = Double.parseDouble(monday.getText());
                    double tues = Double.parseDouble(tuesday.getText());
                    double wed = Double.parseDouble(wednesday.getText());
                    double thurs = Double.parseDouble(thursday.getText());
                    double fri = Double.parseDouble(friday.getText());
                    double sat = Double.parseDouble(saturday.getText());
                    double sun = Double.parseDouble(sunday.getText());

                    private double sum = mon + tues + wed + thurs + fri + sat + sun;

                    JOptionPane.showMessageDialog(null, "Your total is " + sum + "!", "Total", JOptionPane.PLAIN_MESSAGE);

               }
        }
        //THIS IS THE ERROR^

    }



}

请帮忙!任何想法都会奏效。我真的不知道这笔交易是什么:(

4 个答案:

答案 0 :(得分:7)

您正在方法调用中创建一个匿名对象 - 请确保在最后关闭方法调用:

finalize.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent ae){
           // all your stuff
       }
}); // you were missing the ");"

答案 1 :(得分:5)

我认为问题是您需要在第66行的括号后添加);

答案 2 :(得分:3)

缺失 “);”

finalize.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent ae){

                double mon = Double.parseDouble(monday.getText());
                double tues = Double.parseDouble(tuesday.getText());
                double wed = Double.parseDouble(wednesday.getText());
                double thurs = Double.parseDouble(thursday.getText());
                double fri = Double.parseDouble(friday.getText());
                double sat = Double.parseDouble(saturday.getText());
                double sun = Double.parseDouble(sunday.getText());

                private double sum = mon + tues + wed + thurs + fri + sat + sun;

                JOptionPane.showMessageDialog(null, "Your total is " + sum + "!", "Total", JOptionPane.PLAIN_MESSAGE);

           }
    });

答案 3 :(得分:1)

您没有关闭使用finalize.addActionListener(打开的括号,而您缺少分号。