我正在尝试读取文件并将其显示在样式小部件中。但是当我在控制台中显示输出时,它带有缩进和换行符,但是当我尝试在小部件中显示它时,输出会连续出现。
预期产出:
void add(int x,int y){
int z=x+y;
System.out.println("The added value is:"+z);
}
样式文本内的实际输出:
void add(int x,int y){int z=x+y; System.out.println("The added value is:"+z); }
我尝试过谷歌搜索,但我还没有找到解决方案。请给我建议。
更新:
StyledText editor = new StyledText(viewparent, SWT.MULTI | SWT.BORDER | SWT.WRAP |SWT.V_SCROLL);
BufferedReader buff = new BufferedReader(new FileReader(file));
String str;
System.out.println("File contents :");
while ((str = buff.readLine()) != null) {
Text.append(str);
System.out.println(str);
}
BufferedReader
的readLine
方法不包括它返回的文本中的行尾。因此,当您将文本添加到StyledText
时,您需要添加新行:
editor.append(str);
editor.append('n');