[英]Error: Main method not found in class mainGUI, please define the main method as: public static void main(String[] args)
I am creating a GUI in java and am getting the following error: 我正在用Java创建GUI并收到以下错误:
Error: Main method not found in class mainGUI, please define the main method as:
public static void main(String[] args)
I cant work out why this is happening, considering that i do have a main method in my code and it does contain some code. 考虑到我的代码中确实有main方法,并且确实包含一些代码,因此我无法弄清楚为什么会这样。 My GUI code is as follows: 我的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();
}
}
Could anyone shed some light on as to why this is happening? 有人能阐明为什么会这样吗?
Thanks very much :) 非常感谢 :)
Your code works fine, try running it from command line, or try reinstalling eclipse, because its not a code error, its an environmental error. 您的代码运行良好,请尝试从命令行运行它,或者尝试重新安装eclipse,因为它不是代码错误,而是环境错误。
If that doesn't work, try recreating it, starting from main, then walk your way up, it should pinpoint where eclipse is acting up. 如果这不起作用,请尝试从main开始重新创建它,然后逐步向上,它应该指出日食在哪里起作用。
good luck! 祝好运!
You know, I came here looking for an answer to a similar problem. 你知道,我来这里是为了寻找类似问题的答案。 Turns out an older incorrect version of a java file with the same name was in my Java home directory. 原来我的Java主目录中有一个同名的Java文件的较旧的不正确版本。 The correct file was saved in some other dir. 正确的文件已保存在其他目录中。 Perhaps you are experiencing a similar problem. 也许您遇到了类似的问题。
Edit: I see this post is several years old. 编辑:我看到这篇文章已经有好几年了。 OP is probably a CIO by now. OP现在可能是CIO。 Leaving comment for those of you who go saving files to odd places. 给那些将文件保存到奇怪位置的人留下评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.