【问题标题】:error: class, interface, or enum expected when compiling错误:编译时需要类、接口或枚举
【发布时间】:2015-07-20 02:05:06
【问题描述】:
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 次大括号,但在同一部分我仍然遇到同样的错误。

【问题讨论】:

  • 整个 buildButtonPanel 类的末尾没有右括号。错误出现在哪里?
  • 从 public void buildButtonPanel 开始。如果有帮助,则错误指示符位于第一行的 v in void 下方,然后继续 calcButton = new JButton("Calculate Charges");
  • 文件中是否还有其他包含此类的内容?问题可能来自上课前的某些事情。你有没有看到我说过你在课程结束时没有右括号?
  • 是的,我添加了它,但仍然收到错误消息。没有其他地方使用该类。

标签: java class interface enums


【解决方案1】:

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

它需要放在这里,因为你不能在方法中包含类。

  buttonPanel.add(exitButton);
}

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

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

public void actionPerformed(ActionEvent e) {
  // Your code goes here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多