You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Java GUI游戏代码时出现‘Editor does not contain main class’错误求助

问题:Eclipse提示“Editor does not contain main class”但代码包含main方法

我正在开发一款两名玩家掷两枚骰子、经过5轮后得分最高者获胜的游戏。编写的GUI代码中包含main方法,但运行测试GUI功能时反复出现“Editor does not contain main class”错误。已将代码写入单独文件,且多次重启Eclipse,问题仍未解决,也未找到有效的在线解决方案。

以下是我的代码:

package game;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI {

    private JFrame frame;
    private JTextField textField;

    // Launch the application.
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI window = new GUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Create the application.
    public GUI() {
        initialize();
    }

    //Initialise the contents of the frame.
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 589, 402);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JLabel enterName = new JLabel("Player1 enter your name");
        enterName.setFont(new Font("Tahoma", Font.PLAIN, 32));
        enterName.setBounds(76, 11, 387, 93);
        frame.getContentPane().add(enterName);
        
        textField = new JTextField();
        textField.setFont(new Font("Tahoma", Font.PLAIN, 20));
        textField.setBounds(86, 99, 381, 35);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        
        JButton confirmButton = new JButton("Confirm name");
        confirmButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String p1 = " ";
                String p2 = " ";
                int count = 0;
                if (count == 0) {
                    p1 = textField.getText();
                    textField.setText("");
                    System.out.println(p1);
                }
                if (count > 1) {
                    p2 = textField.getText();
                    System.out.println(p2);
                }
                System.out.println(p1 + p2);
                enterName.setText("Player2 enter your name");
                count = count+1;
            }
        });
        confirmButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
        confirmButton.setBounds(184, 145, 185, 35);
        frame.getContentPane().add(confirmButton);
        
        JLabel winnerText = new JLabel("");
        winnerText.setFont(new Font("Tahoma", Font.PLAIN, 38));
        winnerText.setBounds(76, 191, 468, 38);
        frame.getContentPane().add(winnerText);
    }
}

错误排查步骤

  • 检查项目构建路径:右键项目 → Build Path → Configure Build Path,确认src目录被标记为Source Folder,未被排除在外。
  • 排查编译错误:确保代码无语法错误、缺失依赖或import语句,编译错误会导致Eclipse无法识别main方法。
  • 重置运行配置:右键GUI类 → Run As → Run Configurations,删除旧的运行配置,重新创建Java Application配置,指定主类为game.GUI。
  • 清理重建项目:点击Eclipse菜单栏的Project → Clean,选择当前项目清理后重新构建。
  • 验证文件路径:确保GUI.java文件位于src/game/目录下,包名game与目录结构完全匹配。

附带代码逻辑修正提示

原代码中confirmButton的ActionListener里,count是局部变量,每次点击都会重置为0,导致永远无法获取Player2的名字;同时判断Player2的条件count > 1逻辑错误,应改为count == 1。可将count改为GUI类的成员变量,解决该问题。

内容的提问来源于stack exchange,提问作者CaptainBread42

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:15:03