简体   繁体   English

错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)

[英]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 . 例如,请在这里看看。

see this and this for more help 看到这个这个以获得更多帮助

You have to add your main() as: 您必须将main()添加为:

add(jp);
}
public static void main(String[] args){
//Call constructor
}

}  

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.

相关问题 错误:在类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) “错误:在Grad类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)” - “Error: Main method not found in class Grad, please define the main method as: public static void main(String[] args)” 错误:在 class 中找不到主要方法,请将主要方法定义为:public static void main(String[] args) - Error: Main method not found in class, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args) 错误:在TextBook类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args) 在ActivityTime类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Main method not found in class ActivityTime, please define the main method as: public static void main(String[] args) 在 class _____ 中找不到主要方法。 请将主要方法定义为:public static void main(String[] args) - Main Method not found in class _____. Please define the main method as: public static void main(String[] args) 错误:在类 singleInheritance.A 中找不到 Main 方法,请将 main 方法定义为:public static void main(String[] args) 或 JavaFX - Error: Main method not found in class singleInheritance.A, please define the main method as: public static void main(String[] args) or a JavaFX Eclipse-错误:在类projectOne中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Eclipse - Error: Main method not found in class projectOne, please define the main method as: public static void main(String[] args) ECLIPSE:错误:在类apple中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - ECLIPSE : Error: Main method not found in class apple, please define the main method as: public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM