Javafx标签丢失文本格式

时间:2015-12-01 20:31:01

标签: javafx label javafx-8

目的只是从文件中抓取一些已经间隔开的文本,并显示标签中的所有行。但是,即使文本在文件中间隔正确并且如此读取,在放入标签时似乎也会丢失间距,我无法弄清楚原因。

Text in file

How it appears in the label

以下是从文件中读取并创建标签

的代码段
//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();

0 个答案:

没有答案