【问题标题】:Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args)错误:在 mainGUI 类中找不到主方法,请将主方法定义为:public static void main(String[] args)
【发布时间】:2014-05-13 00:28:45
【问题描述】:

我在 java 中创建 GUI 并收到以下错误:

Error: Main method not found in class mainGUI, please define the main method as:
public static void main(String[] args)

考虑到我的代码中确实有一个 main 方法并且它确实包含一些代码,我无法弄清楚为什么会发生这种情况。我的GUI代码如下:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class mainGUI extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private static final int WIDTH = 400;
private static final int HEIGHT = 300;

private JLabel lengthL, widthL, areaL, perimeterL;
private JTextField lengthTF, widthTF, areaTF, perimeterTF;
private JButton calculateB, exitB;

private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

public mainGUI() {

    lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
    widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
    areaL = new JLabel("Area: ", SwingConstants.RIGHT);
    perimeterL = new JLabel("Perimeter: ", SwingConstants.RIGHT);

    lengthTF = new JTextField(10);
    widthTF = new JTextField(10);
    areaTF = new JTextField(10);
    calculateB = new JButton("Calculate");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);
    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    lengthTF = new JTextField(10);
    widthTF = new JTextField(10);
    areaTF = new JTextField(10);
    perimeterTF = new JTextField(10);

    calculateB = new JButton("Calculate");
    exitB = new JButton("Exit");
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(5, 2));

    pane.add(lengthL);
    pane.add(lengthTF);
    pane.add(widthL);
    pane.add(widthTF);
    pane.add(areaL);
    pane.add(areaTF);
    pane.add(calculateB);
    pane.add(exitB);

    setTitle("Main Menu");
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            double width, length, area;
            length = Double.parseDouble(lengthTF.getText()); //We use the     getText & setText methods to manipulate the data entered into those fields.
            width = Double.parseDouble(widthTF.getText());
            area = length * width;           
            areaTF.setText("" + area);
        }
    }

public class ExitButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
}

public static void main(String[] args) {
        mainGUI rectObj = new mainGUI();
}     
}

谁能解释一下为什么会这样?

非常感谢:)

【问题讨论】:

  • 你是如何启动你的应用程序的?
  • 通过eclipse中的控制台
  • 我可以毫无问题地从 Eclipse 启动该代码...
  • 如果这确实是您创建的 exact 代码,那么它根本不应该在 Eclipse 或其他方式中运行——您缺少一个右花括号。
  • 你是在运行同一个类还是你有另一个同名的类没有主方法

标签: java compiler-errors main


【解决方案1】:

您的代码工作正常,尝试从命令行运行它,或者尝试重新安装 eclipse,因为它不是代码错误,而是环境错误。

如果这不起作用,请尝试重新创建它,从 main 开始,然后向上走,它应该可以查明 eclipse 发生的位置。

祝你好运!

【讨论】:

    【解决方案2】:

    你知道,我来这里是为了寻找类似问题的答案。结果发现我的 Java 主目录中有一个旧的不正确版本的同名 java 文件。正确的文件保存在其他目录中。也许您遇到了类似的问题。

    编辑:我看到这篇文章已经有好几年了。 OP 现在可能是 CIO。为那些将文件保存到奇怪的地方的人发表评论。

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 2014-01-13
      • 2017-03-04
      • 2015-05-20
      • 1970-01-01
      • 2015-11-16
      • 2017-03-01
      • 1970-01-01
      相关资源
      最近更新 更多