简体   繁体   English

在JTextArea中启用更智能的文本换行

[英]enabling smarter text wrapping in a JTextArea

The Context 上下文

At the moment, I have a JTextArea that has been created like so: 目前,我已经创建了一个JTextArea,如下所示:

JTextArea descArea = new JTextArea();
descArea.setFont(style.getFont());
descArea.setLineWrap(true);
descArea.setName("descArea");
descArea.setToolTipText(resourceMap.getString("descArea.toolTipText"));
descArea.setText(model.getName());
JScrollPane descPane = new JScrollPane(descArea,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

When a user types something into the field, it does indeed wrap (as per descArea.setLineWrap(true) ), but does it a little clumsily by breaking words like the following example: 当用户在该字段中键入内容时,它确实会自动换行(按照descArea.setLineWrap(true) ),但是会像下面的示例一样通过打断单词来使它descArea.setLineWrap(true)笨拙:

替代文字

The users of our software are expecting the wrapping to be a little more clever and automatically generate something more like: 我们软件的用户期望包装更加巧妙,并会自动生成如下内容:

替代文字

With the general idea being that when they type the last 'th' it all moves down to the second line along with the insertion point as they type (in a way similar to just about every other text editor). 通常的想法是,当他们键入最后一个“ th”时,它们都会随着键入的内容一起向下移动到第二行以及插入点(其方式几乎与其他所有文本编辑器一样)。


The Question 问题

My initial thought had been to implement this sort of wrapping manually using a Key Listener, but I was wondering if there is a better approach/different component that could achieve this functionality more easily? 我最初的想法是使用Key Listener手动实现这种包装,但是我想知道是否有更好的方法/不同组件可以更轻松地实现此功能?

它已经实现。

  textArea.setWrapStyleWord(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM