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

时间:2014-01-19 04:04:03

标签: java

我是编码的初学者并遇到了这个问题。

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

我只是不知道我会在哪里解决它。

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);

   }

}  

3 个答案:

答案 0 :(得分:3)

JVM正在告诉你到底出了什么问题:如果没有main方法,你就无法运行一个类 - 所以给它一个。请查看任何开头的Java书籍或教程,因为这是第一章中常见的内容。例如,请查看here

答案 1 :(得分:0)

请参阅thisthis以获取更多帮助

您必须将main()添加为:

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

}  

答案 2 :(得分:0)

好吧,从错误消息中可以看出,你错过了java程序中的main()方法。

所以可能的解决方案是添加一个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
   }

}

定义类main()的{​​{1}}方法创建对象并调用构造函数Text

由于您已经提到过您在编码方面的半初学者,您可以通过示例来引用java中构造函数的资源。

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/java-constructor-example/