![](/img/trans.png)
[英]Error: Main method not found in class mainGUI, please define the main method as: 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)
我是一名編碼的初學者,遇到了這個問題。
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);
}
}
JVM恰恰告訴您什么地方出了問題:如果沒有主方法,您將無法運行類,因此請給它一個方法。 請參閱任何Java入門書籍或教程,因為這通常是在第一章中找到的內容。 例如,請在這里看看。
好吧,從錯誤消息中可以明顯看出您錯過了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()
方法之后,創建Text
類的對象並調用構造函數Text()
。
既然您已經說過您是編碼的初學者,那么可以通過示例引用有關Java構造函數的資源。
https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.