简体   繁体   English

错误:找不到或加载主类

[英]Error: Could not find or load main class

I'm new to Java programming and doing this as a project for school. 我是Java编程的新手,并以此作为学校的项目。 Here's the code I've written 这是我写的代码

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);
      }
    }
  }
}

When running I get an error message saying: 运行时,我收到一条错误消息:

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

I've done a search of this site and checked the answers as much as I know how. 我已经搜索了该站点并尽我所能检查了答案。 I would appreciate any help that you offer. 我会很感激您提供的任何帮助。

You have put main method outside the class 您已经将主要方法放在了课外

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

place it inside and remember you cant put anything before these imports 放进去,记住在这些进口之前你不能放任何东西

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

It's a bit hard to tell from your formatting. 从格式上很难分辨出来。 But if you're going to be running a class called "Graduation", you're code is going to have to look like this: 但是,如果要运行一个名为“ Graduation”的类,则代码将必须如下所示:

public class Graduation{ 


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

}

You need the Graduation class, and it needs to have a main method that conforms to that. 您需要Graduation类,并且它需要一个符合该类的main方法。 It's not clear from your example that your methods are actually inside a class. 从您的示例尚不清楚您的方法是否实际上在类内。 Is it compiling? 正在编译吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM