2014-01-19 85 views
0

我是編碼方面的初學者,遇到此問題。錯誤:在類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). 

我只是不知道我會在哪裏修復它。

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

    } 

} 
+0

可能重複[Main方法沒有找到,即使我已經聲明它(http://stackoverflow.com/questions/19339088/main-method -not-found-even-if-ive-declared-it) – RAS

回答

3

JVM告訴你到底發生了什麼問題:你不能在沒有主方法的情況下運行一個類 - 所以給它一個。請看看任何開始的Java書或教程,因爲這是第一章中常見的東西。例如,請看看here

+0

我可以在哪裏添加? – user2997369

+2

@ user2997369:它可以是此類的一種方法,但首先請閱讀鏈接。你有一些閱讀和學習要做。 –

+0

就你而言,它可能屬於同一班。但你也可以把它放在另一個。 – Christian

0

看到thisthis更多的幫助

你必須添加您的main()爲:

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

} 
0

嗯,從錯誤信息很明顯,你已經錯過了在main()方法一個java程序。

所以可能的解決方案是增加一個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()方法之後創建Text類的對象,並調用構造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/

相關問題