我无法解决的奇怪异常

时间:2014-10-16 08:15:42

标签: java exception try-catch curly-braces

import javax.swing.*;
import java.awt.*;
import java.awt.font.*;

public class test {
    int y = (60 * 40);
    public static JLabel timer = new JLabel("");

    public test() {
        JFrame frame = new JFrame("timer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(timer);
        frame.setSize(1000, 500);
        frame.setVisible(true);
        frame.setForeground(Color.black);
        timer.setFont(new Font("Calibri", Font.BOLD, 100));
        timer.setForeground(Color.WHITE);
    }

    public static void main(String args[]) {

        SwingUtilities.invokeAndWait(new Runnable() { //line 29
            public void run() {
                test2 it = new test2();
                Thread wasd = new Thread(it);
                new test();
                wasd.start();
            }
        });
    }
}

由于一些奇怪的原因,编译器说第29行需要有一个try / catch块,但每次尝试都会失败。花括号拒绝与我合作,并最终解析错误,因为它跳过我放在那里的任何尝试块。我不知道如何离开这个。

1 个答案:

答案 0 :(得分:0)

public test() {
    JFrame frame = new JFrame("timer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(timer);
    frame.setSize(1000, 500);
    frame.setVisible(true);
    frame.setForeground(Color.black);
    timer.setFont(new Font("Calibri", Font.BOLD, 100));
    timer.setForeground(Color.WHITE);
}

public static void main(String args[]) {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                test it = new test();
                Thread wasd = new Thread();
                new test();
                wasd.start();
            }

        });
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}