错误:编译时预期的类,接口或枚举

时间:2015-05-09 04:25:06

标签: java class interface enums

public void buildButtonPanel()
  {

     buttonPanel = new JPanel();

     calcButton = new JButton("Calculate Charges");
     exitButton = new JButton("Exit");


     calcButton.addActionListener(new CalcButtonListener());
     exitButton.addActionListener(new ExitButtonListener());


     buttonPanel.add(calcButton);
     buttonPanel.add(exitButton);



        private class CalcButtonListener implements ActionListener
        {

           public void actionPerformed(ActionEvent e)


              total = routine.getRoutineCost() +
                      nonRoutine.getNonRoutineCost();

              DecimalFormat dollar = new DecimalFormat("0.00");

              JOptionPane.showMessageDialog(null, "Total: $" + dollar.format(total));

         }

        private class ExitButtonListener implements ActionListener
        {            

           public void actionPerformed(ActionEvent e)


              System.exit(0);
         }

错误:预期的类,接口或枚举

每行都有相同的错误。

我认为这是一个大括号错误,但我已经重复了3次括号,而且我在同一部分仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:1)

buildButtonPanel需要在类之前有一个右括号。

它需要到这里,因为你不能在方法中有类。

  buttonPanel.add(exitButton);
}

您向我们展示的所有代码是否都包含在课程中? buildButtonPanel是一个方法,必须在一个类中。

你的actionPerformed方法也没有大括号。

public void actionPerformed(ActionEvent e) {
  // Your code goes here
}
相关问题