gpt4 book ai didi

java - 向 java 格式化程序添加格式

转载 作者:行者123 更新时间:2023-12-04 06:23:16 25 4
gpt4 key购买 nike

我正在写一个记事本克隆来了解更多关于 guis 的信息。我的程序使用以下代码创建一个文本文件:

                try {
formatter = new Formatter(fileName+".txt");
formatter.format(contents);
formatter.close();
JOptionPane.showMessageDialog(null, "File saved as "+fileName +".txt");

} catch (Exception e){
JOptionPane.showMessageDialog(null, "Error writing to file");
}

如何在我的文本上保留格式?我正在从 JTextArea 检索文件:
String contents = text.getText();

但基本上我丢失了所有格式。当我从文件中读取时,我会在单词之间重新获得空格:
字符串读取 = sc.next();
opensText = opensText + ""+ 阅读;

反正有没有在字符串中存储格式?

最佳答案

JTextArea 不支持任何“格式化”。您所能做的就是在单词之间添加制表符或空格。如果文本没有按照您期望的方式排列,那么我猜您需要在文本区域中使用“等宽字体”。

textArea.setFont( new Font("monospaced", Font.PLAIN, 10) );

关于java - 向 java 格式化程序添加格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com