[英]Error: Main method not found in class Text, please define the main method as: public static void main(String[] args)
I am a semi beginner at coding and ran into this problem. 我是一名编码的初学者,遇到了这个问题。
Error: Main method not found in class Text, please define the main method as: public static void main(String[] args).
I just don't know where I would fix it. 我只是不知道该在哪里修复它。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Text extends JFrame
{
ImageIcon aries = new ImageIcon ("aries.jpg");
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jt = new JTextField("Month",30);
JTextField jt2 = new JTextField("Date",30);
JButton jb = new JButton("Enter");
public Text()
{
setTitle("Tutorial");
setVisible(true);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp.add(jt);
jp.add(jt2);
jt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
jl.setText(input);
}
});
jp.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
String input2 = jt2.getText();
jl.setText(input);
jl.setText(input2);
int day = Integer.parseInt(input2);
if ((input.equals("Test")) && (input2.equals(day >= 26)))//||(input2.equals("27")))))
JOptionPane.showMessageDialog(null, "" , "" ,JOptionPane.PLAIN_MESSAGE,aries);
}
});
add(jp);
}
}
The JVM is telling you exactly what is wrong: You can't run a class without a main method -- so give it one. JVM恰恰告诉您什么地方出了问题:如果没有主方法,您将无法运行类,因此请给它一个方法。 Please have a look at any beginning Java book or tutorial since this is stuff usually found in the first chapter. 请参阅任何Java入门书籍或教程,因为这通常是在第一章中找到的内容。 For example, please have a look here . 例如,请在这里看看。
Well, from the error message it is evident that you have missed out the main()
method in a java program. 好吧,从错误消息中可以明显看出您错过了Java程序中的main()
方法。
So the possible solution is to add a main() method, like this 所以可能的解决方案是添加一个main()方法,像这样
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Text extends JFrame
{
ImageIcon aries = new ImageIcon ("aries.jpg");
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jt = new JTextField("Month",30);
JTextField jt2 = new JTextField("Date",30);
JButton jb = new JButton("Enter");
public Text()
{
setTitle("Tutorial");
setVisible(true);
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp.add(jt);
jp.add(jt2);
jt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
jl.setText(input);
}
});
jp.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = jt.getText();
String input2 = jt2.getText();
jl.setText(input);
jl.setText(input2);
int day = Integer.parseInt(input2);
if ((input.equals("Test")) && (input2.equals(day >= 26)))//||(input2.equals("27")))))
JOptionPane.showMessageDialog(null, "" , "" ,JOptionPane.PLAIN_MESSAGE,aries);
}
});
add(jp);
}
public static void main(String[] args)
{
// create object of class Text
// now call constructor
}
}
After defining the main()
method create object of class Text
and call constructor Text()
. 定义main()
方法之后,创建Text
类的对象并调用构造函数Text()
。
Since you have mentioned that you're semi beginner at coding, you can refer the resources on constructor in java with example. 既然您已经说过您是编码的初学者,那么可以通过示例引用有关Java构造函数的资源。
https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html
http://www.flowerbrackets.com/learn-constructor-in-java/ http://www.flowerbrackets.com/learn-constructor-in-java/
http://www.flowerbrackets.com/java-constructor-example/ http://www.flowerbrackets.com/java-constructor-example/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.