当指定类型时,javac“使用未经检查或不安全的操作”

时间:2021-11-05 15:50:32

The following code:

以下代码:

public void addGrillaListener(Stage stageToClose,Grilla listener)
{
    GrillaHandler<WindowEvent> handy = new GrillaHandler<>(listener);
    if(stageToClose!=null)
    {
        stageToClose.addEventHandler(WindowEvent.WINDOW_HIDDEN,handy);
    }
}

causes the compiler to show that message. How can I avoid it?

导致编译器显示该消息。我怎么能避免呢?

Extra info:

额外信息:

  • Grilla is a Stage interface
  • Grilla是舞台界面
  • GrillaHandler is a EventHandler subclass that takes a Grilla as a constructor parameter
  • GrillaHandler是一个EventHandler子类,它将Grilla作为构造函数参数
  • Using JDK 7, GrillaHandler<> is allowed
  • 使用JDK 7,允许GrillaHandler <>
  • The compiler message is rather unespecific - it states that this method uses unchecked or unsafe operations
  • 编译器消息是非特定的 - 它声明此方法使用未经检查或不安全的操作
  • Stage is a class provided by oracle, it's part of javafx
  • Stage是oracle提供的一个类,它是javafx的一部分

GrillaHandler:

GrillaHandler:

public class GrillaHandler<T> implements EventHandler {

    private Grilla win;

    public GrillaHandler(Grilla win) {
        this.win=win;
    }

    @Override
    public void handle(Event t) {
        win.loadTable();
    }
}

Grilla:

格里拉:

public interface Grilla { 
    public void loadTable();
}

1 个解决方案

#1


5  

Change code to

将代码更改为

public class GrillaHandler<T extends Event> implements EventHandler<T>{ 
//...
}

The JavaFX EventHandler is a paremeterized type. You are missing that one in your declaration of the GrillaHandler. You are forced to provide a type argument in your class declaration or redeclare the type parameter, as you seem to require as per your declaration.

JavaFX EventHandler是一种paremeterized类型。您在GrillaHandler的声明中缺少那个。您*在类声明中提供类型参数或重新声明类型参数,因为您似乎需要根据您的声明。

#1


5  

Change code to

将代码更改为

public class GrillaHandler<T extends Event> implements EventHandler<T>{ 
//...
}

The JavaFX EventHandler is a paremeterized type. You are missing that one in your declaration of the GrillaHandler. You are forced to provide a type argument in your class declaration or redeclare the type parameter, as you seem to require as per your declaration.

JavaFX EventHandler是一种paremeterized类型。您在GrillaHandler的声明中缺少那个。您*在类声明中提供类型参数或重新声明类型参数,因为您似乎需要根据您的声明。