获取主机服务();未找到方法。

getHostServices(); Method not found.

我想使用下面的代码打开 pdf 文件,但是方法 getHostServices();即使我有正确的导入也没有找到。

import javafx.application.HostServices;

ClinicFiles clinicFiles = (ClinicFiles) listViewClinic.getSelectionModel().getSelectedItem(); 
HostServices hostServices = getHostServices();
hostServices.showDocument(clinicFiles.getAbsolutePath());

可能,您的 class 没有从 javafx.application.Application:

延伸

例如:

import javafx.application.Application;
import javafx.application.HostServices;

public class App extends Application {
    public void method(String[] args) {
        ClinicFiles clinicFiles = (ClinicFiles) listViewClinic.getSelectionModel().getSelectedItem();
        HostServices hostServices = getHostServices();
        hostServices.showDocument(clinicFiles.getAbsolutePath());
    }
}