1

它从哪里开始java.awtpublic void run()它们都带有红色下划线,当我用鼠标单击它们时,我收到一条消息,提示将初始化程序移动到构造函数。谁能帮我这个?

public static void main (String[] args) {
    // TODO code application logic here
    EmployeeRecord main = new EmployeeRecord() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EmployeeRecord().setVisible(true);
            }
        )}
    }
}
4

1 回答 1

1

取出包装纸。以下是您所需要的。

public static void main (String[] args) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new EmployeeRecord().setVisible(true);
        }
    });    // <======= Notice the change here too. 
}

你只需要静态调用类的invokeLater方法EventQueue。做你正在做的事情是一个完全不同的(非法)结构,这甚至是不可能的。您基本上是在创建一个匿名类实例,并在其中调用相同的构造函数。即使它是一个正确的构造,例如

public static void main (String[] args) {
    // TODO code application logic here
    EmployeeRecord main = new EmployeeRecord() {
        {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new EmployeeRecord().setVisible(true);
                }
            });
        }
    };
} 

您将创建一个不必要的实例。

于 2014-09-06T11:52:34.157 回答