gpt4 book ai didi

java - 错误: Main method not found in class mainGUI,请将main方法定义为:public static void main(String[] args)

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:20 24 4
gpt4 key购买 nike

我正在用 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 开始,然后向上走,它应该查明 eclipse 发生故障的位置。

祝你好运!

关于java - 错误: Main method not found in class mainGUI,请将main方法定义为:public static void main(String[] args),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799317/

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