【问题标题】:Getting error message for static main [closed]获取静态主错误消息[关闭]
【发布时间】:2017-01-05 20:16:28
【问题描述】:

这是我在尝试编译时在 cmd 中收到的错误消息... 错误:无效的方法声明;需要返回类型 静态主(字符串 args[])

这是我的代码(错误出现在最后的声明中)。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JWindow;

public class Counter extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    private final font FONT = new Font("Impact", Font.PLAIN, 72);
    private final File SOUND = new File("sound/tick.wav");

    private Timer timer;
    private int time;

    public Counter() {
        set0paque(false);
        setPrefferedSize(new Dimension(400,400));
        time = 60;
        timer = new Timer(1000, this);
        timer.start();
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints,KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setFont(FONT);
        g2.setColor(Color.GREEN);
        String text = "00:" +String.valueOf(time);
        int width = g.getFontMetrcis().stringWidth(text);
        g2.drawString(text, getWidth() / 2 - width / 2, getHeight() / 2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        time--;

        if(time == 0) {
            shutdown();
        }
        repaint();
        playsound();

    }

    private void shutdown() {
        try{
            Runtime runtime =  Runtime.getRuntime();
            runtime.exec("shutdown -s -t 0");
            Systen.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static main(String args[]) {
        JWindow window = new JWindow();
        window.add(new Counter());
        window.pack();
        window.setBackground(new Color(0,0,0,0));
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}   

谢谢:

【问题讨论】:

  • public static main(String args[]) { --> public static void main(String args[]) { 你忘记了返回类型。
  • 错误信息告诉你问题出在哪里:你没有为你的main()方法指定一个返回类型。假设您希望能够将该方法用作应用程序的入口点,那么您想要的返回类型是void
  • 看起来你可以自己解决这个问题。当您看到错误消息时不要只是惊慌失措,而是停止仔细查看它并考虑它可能在说什么。您还可以在 Google 上搜索其他带有 main 的 Java 程序的任何示例,例如 Google Java main 方法,然后将该示例与您自己的代码进行比较。不要懒惰、粗心或草率下结论。这将极大地帮助您培养耐心、专注和注意力以及对细节的关注。

标签: java static syntax-error main public


【解决方案1】:

每个方法声明都必须有一个返回类型。如果您不想返回任何内容(如main 的情况),则应将返回类型声明为void

public static void main(String args[]) {
    // Here --^

【讨论】:

  • 然后我收到 19 条新的错误消息:o
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2018-06-11
  • 2011-12-08
  • 2015-08-19
相关资源
最近更新 更多