将多行 JTextArea 写入 txt 文件

发布于 2024-12-08 12:08:51 字数 874 浏览 2 评论 0原文

所以我有一个用于用户输入的JTextArea,然后当他们单击按钮时,它会将其写入文本文件,我有setLineWrapsetWrapStyleWord对于 JTextArea 设置为 true

我想按照文本框中显示的方式写入文本文件。

我尝试在保存 JTextArea 内容的 String 上执行 replace("\n", System.getProperty("line.separator")) 有效,但前提是用户在输入内容时确实按下了回车键,但如果用户只是继续输入并按下一行的末尾,因此它会跳转到其下方的行,则 replace 不起作用。

我还尝试过使用 StringBuffer,我计算了一行中可以容纳多少个字符,然后运行一个 for 循环 插入 新行每行的末尾,因此对于第一行,我将其添加到位置 90,第二行添加到 180,第三行添加到 270,依此类推。但我很快意识到这并不适用于所有情况,因为有些字符比其他字符小,例如您可以在一行中容纳比 p 更多的 j 字符。

我想做的只是找出一种计算行尾的方法,这样我就知道在哪里插入新行,但我也愿意接受其他建议。谢谢。如果您认为查看我的一些代码会有帮助,请询问。

更新 我没有实现代码来做到这一点,而是与一个人交谈,他几乎是唯一使用该程序的人之一,他对常规记事本格式很好,我没有意识到这一点,但只要他在记事本中检查了 wordWrap格式设置,这样他就不必使用侧滚动来阅读整行,感谢您的输入

so I Have a JTextArea for user input and then when they click a button it writes it to a text file, I have both setLineWrap and setWrapStyleWord set to true for the JTextArea.

I would like to write to the text file the exact way it appears in the text box.

I have tried to do a replace("\n", System.getProperty("line.separator")) on the String holding the contents of the JTextArea which works, but only if the user actually hits the return key when typing the input, but if the user just keeps typing and hits the end of a line so it jumps to the line below it, the replace doesnt work.

I have also tried using a StringBuffer, I calculated how many characters would fit in on line and then ran a for loop inserting new lines at the end of each line so for the first line I would add it at position 90, 2nd line at 180, third at 270 and so on. But I soon realized that does not work in all situations because some characters are smaller than others for example you could fit more j's on a single line than p's.

What I would like to do is just figure out a way calculate the end of a line so I know where to insert the new line, but I am open to other suggestions as well. Thanks. If you think it would help to see some of my code just ask.

Update
Instead of implementing code to do this, I talked to the person who will be pretty much one of the only people using this program and he was fine with regular notepad format and I didnt realize it but as long as he has wordWrap checked in his notepad format settings then that will eliminate him having to use the side scroll in order to read the entire line, Thanks for your input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-12-15 12:08:51

使用 实用程序 类,

public static final int getRowStart(JTextComponent c, int offs)
public static final int getRowEnd(JTextComponent c, int offs)

您传递偏移量并获取行开始和结束的偏移量

Use Utilities class

public static final int getRowStart(JTextComponent c, int offs)
public static final int getRowEnd(JTextComponent c, int offs)

you pass offset and get offset of the row start and end

萝莉病 2024-12-15 12:08:51

我不知道如何做,除了使用字体度量计算字长并执行文本区域已经执行的操作。

但这背后的目的是什么?

I don't see how, except to just calculate word lengths using font metrics and do what the text area already does.

What's the purpose behind this, though?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文