Javafx标签丢失文本格式

Javafx标签丢失文本格式,javafx,label,javafx-8,Javafx,Label,Javafx 8,其目的只是从文件中获取一些已经隔开的文本,并显示标签中的所有行。然而,即使文本在文件中有适当的间距,并且是这样读取的,当放入标签时,它似乎会丢失间距,我也不明白为什么 下面是从文件读取并创建标签的代码段 //read through each line in the file and add it to the string String filename = recordsListView.getSelectionModel().getSelectedItem().toString

其目的只是从文件中获取一些已经隔开的文本,并显示标签中的所有行。然而,即使文本在文件中有适当的间距,并且是这样读取的,当放入标签时,它似乎会丢失间距,我也不明白为什么

下面是从文件读取并创建标签的代码段

//read through each line in the file and add it to the string
    String filename = recordsListView.getSelectionModel().getSelectedItem().toString();
    String filecontents = "";
    try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
        String line;
        while ((line = br.readLine()) != null) {
            filecontents+=line+"\n";
        }
    }
    catch (Exception e) 
    {
        filecontents = "File corrupt or not found.";
    }
    //create the window to show
    Stage dialog = new Stage();
    dialog.setTitle("File: "+filename);
    //utility style has the basic window decorations
    dialog.initStyle(StageStyle.UTILITY);
    Label label = new Label();
    label.setText(filecontents);
    //set id to mark for a css property
    label.setId("popupLabel");
    ScrollPane sp = new ScrollPane();
    sp.setContent(label);
    Scene scene = new Scene(sp);
    scene.getStylesheets().clear();
    scene.getStylesheets().add(getClass().getResource(cssSheet).toExternalForm());
    dialog.setScene(scene);
    dialog.setWidth(300);
    dialog.setHeight(250);
    //NONE allows the window to be treated as a seperate window, and the windows underneath can still have events triggered
    dialog.initModality(Modality.NONE);
    dialog.show();

这不就是因为字体吗?(也就是说,你的文件查看器使用的是单间距字体,而你的标签不是吗?)。非常感谢你!