簡體   English   中英

錯誤:在類mainGUI中找不到主要方法,請將該主要方法定義為:public static void main(String [] args)

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

我正在用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,因為它不是代碼錯誤,而是環境錯誤。

如果這不起作用,請嘗試從main開始重新創建它,然后逐步向上,它應該指出日食在哪里起作用。

祝好運!

你知道,我來這里是為了尋找類似問題的答案。 原來我的Java主目錄中有一個同名的Java文件的較舊的不正確版本。 正確的文件已保存在其他目錄中。 也許您遇到了類似的問題。

編輯:我看到這篇文章已經有好幾年了。 OP現在可能是CIO。 給那些將文件保存到奇怪位置的人留下評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM