有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java奇怪的异常,我无法解决

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块,但每次我尝试时。。。。它失败了。花括号拒绝与我合作,最终导致解析错误,因为它跳过了我在其中放入的try块。我不知道该怎么办


共 (1) 个答案

  1. # 1 楼答案

    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();
        }
    
    }
    

    }