gpt4 book ai didi

java - 错误: Could not find or load main class

转载 作者:行者123 更新时间:2023-12-01 14:41:43 26 4
gpt4 key购买 nike

我是 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.

我已经搜索了这个网站并尽可能多地检查了答案。我将不胜感激您提供的任何帮助。

最佳答案

您已将 main 方法放在类之外

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

将其放入其中并记住在这些导入之前不能放置任何东西

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

关于java - 错误: Could not find or load main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15890233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com