获取静态main的错误消息

时间:2017-01-05 20:16:28

标签: java static syntax-error main public

每当我尝试编译它时,这是我在cmd中收到的错误消息... 错误:方法声明无效;需要返回类型 static main(String 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);
    }
}   

谢谢你:

1 个答案:

答案 0 :(得分:2)

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

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