错误:找不到或加载主类

时间:2013-04-08 22:34:30

标签: java

我是Java编程的新手,并将其作为学校项目。这是我写的代码

public static void main(String args[])
import javax.swing.*;
import java.awt.event.*;
public class SleepCounter extends JFrame {
  private JPanel panel;
  private JLabel messageLabel;
  private JTextField sleepTextField;
  private JTextField sleepAnswerField;
  private final int WINDOW_WIDTH = 310;
  private final int WINDOW_HEIGHT = 100;
  public SleepCounter() {
    setTitle("Sleep Counter");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
  }
  private void buildPanel() {
    dailyLabel = new JLabel("Enter Sleep " + "in hours");
    hourTextField = new JTextField(10);
    CalcButton = new JButton("Calculate");
    CalcButton.addActionListener(new CalcButtonListener());
    panel = new JPanel();
    panel.add(dailyLabel);
    panel.add(sleepTextField);
    panel.add(CalcButton);
    panel.add(sleepAnswerField);
  }
  private class CalcButtonListener implements ActionListener {
    public void actionPerformer(ActionEvent e) {
      String input;
      int total;
      input = sleepTextField.getText();
      for (int i = 0; i < 7; i++); {
        input += total;
      }
      JOptionPane.showMessageDialog(null, "The total amount of sleep for " + (i + 1) + "days is" + total);
      if (int i > 7) {
        double avg = (total / 7);
        JOptionPane.showMessageDialog(null, "The avg amount of sleep for 7 days is" + avg);
      }
    }
  }
}

运行时,我收到一条错误消息:

Error: Could not find or laod main class Graduation Project.

我已经对这个网站进行了搜索,并根据我的了解检查了答案。我很感激您提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

您已将主要方法放在课堂外

 public class  YourClass
 {
  public static void main(String args[])
   {  
   }
  }

把它放在里面,记住你不能在这些进口之前放任何东西

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

答案 1 :(得分:0)

从格式化来看,这有点难以理解。但如果您要运行一个名为“Graduation”的课程,那么您的代码必须如下所示:

public class Graduation{ 


  public static void main (String[] args){
  }

}

你需要毕业课,它需要有一个符合的方法。从你的例子中不清楚你的方法实际上是在一个类中。它在编译吗?