java - "Class does not have a main method"

标签 java

<分区>

所以我有一个程序可以计算指数增长,它看起来应该可以工作...但是当我尝试在 NetBeans 中运行它时,它说类没有主要方法。还有一个问题,当我编译一个文件时,它会保存在哪里,它是否可以运行?感谢任何答案。这是代码

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Answerfinder extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHTH = 300;

private JLabel Ial, Ratelabel,earn1, earn2, earn3, earn4, earn5, earn10;
private JLabel equalabel;
private JTextField ratein,initialamin, earn1out, earn2out, earn3out, earn4out, earn5out, earn10out;
private JButton calculatebut;
private CalculateButtonHandler cbhandler;

public static void main(String args) {
    Answerfinder find = new Answerfinder();

}

public Answerfinder() {

    // adding the labels
    Ial = new JLabel("Enter your initial amount:",SwingConstants.RIGHT);
    Ratelabel = new JLabel("Enter growth percentage (just numbers):",SwingConstants.RIGHT);
    earn1 = new JLabel("Total by year 1:",SwingConstants.RIGHT);
    earn2 = new JLabel("Total by year 2:",SwingConstants.RIGHT);
    earn3 = new JLabel("Total by year 3:",SwingConstants.RIGHT);
    earn4 = new JLabel("Total by year 4:",SwingConstants.RIGHT);
    earn5 = new JLabel("Total by year 5:",SwingConstants.RIGHT);
    earn10 = new JLabel("Total by year 10:",SwingConstants.RIGHT);
    equalabel = new JLabel("Press this to calculate: ");

    //adding the textfields
    ratein = new JTextField();
    initialamin = new JTextField();
    earn1out = new JTextField();

    //adding button
    calculatebut = new JButton("Calculate");
    cbhandler = new CalculateButtonHandler();
    calculatebut.addActionListener(cbhandler);

    // Sets title of program
    setTitle("Exponential Growth Calculator");
   // Sets size of frame, whether or not it's visible, and what do on close
    setSize(WIDTH, HEIGHTH);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // establishes container and sets the layout
    Container pane = getContentPane();
    pane.setLayout(new GridLayout (8,2));

    pane.add(Ial);
    pane.add(initialamin);
    pane.add(Ratelabel);
    pane.add(ratein);
    pane.add(equalabel);
    pane.add(calculatebut);
    pane.add(earn1);
    pane.add(earn1out);
    pane.add(earn2);
    pane.add(earn2out);
    pane.add(earn3);
    pane.add(earn3out);
    pane.add(earn4);
    pane.add(earn4out);
    pane.add(earn5);
    pane.add(earn5out);
    pane.add(earn10);
    pane.add(earn10out);
}

private class CalculateButtonHandler implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent e) {

        double rate, initamount, returns1, returns2, returns3, returns4, returns5, returns10;

        rate = Double.parseDouble(ratein.getText());
        if (rate > 1) {
            rate = (rate / 100) + 1;
        } else {
            rate = rate +1;
        }
        initamount = Double.parseDouble(initialamin.getText());
        returns1 = initamount * Math.pow(rate, 1);
        returns2 = initamount * Math.pow(rate, 2);
        returns3 = initamount * Math.pow(rate, 3);
        returns4 = initamount * Math.pow(rate, 4);
        returns5 = initamount * Math.pow(rate, 5);
        returns10 = initamount * Math.pow(rate, 10);
    }
}

最佳答案

你有:

public static void main(String args) {

你的意思是:

public static void main(String[] args) {

或者:

public static void main(String... args) {


main 方法必须采用参数数组。具体来说,来自 JLS 12.1.4 :

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)

public static void main(String... args)

关于java - "Class does not have a main method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22314704/

相关文章:

java - 尝试在 Java 中使用递归任务(Fork 和 Join)计算 (3*3)^2 时出错

java - 我找不到我的 java.util.ConcurrentModificationException 的原因

java - 如何将十六进制转换为以十进制打印时看起来相同的数字?

java - CDI + Java EE 7 中的托管并发

java - 使用 0,1 而不是 ""替换字符串的一部分

java - 如何使用 lambda 获取 HashMap 中值的键数

java - Java 的 Arrays.asList() 是否违反了 OOP?

Linux 上的 Java 套接字错误(发送 0xFF,接收到 -3)

java - 设置 JSpinner 倒计时器

java - Tomcat Maven 插件 - 子容器在启动期间失败