gpt4 book ai didi

java - Swing - 我的类扩展了 JTextPane 并首先调用我的append(),然后调用 super.setText() 添加额外的换行符

转载 作者:行者123 更新时间:2023-12-02 13:12:03 24 4
gpt4 key购买 nike

足够简单:我有这个 SSCCE:

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import net.miginfocom.swing.MigLayout;

public class Test1_ChangeStyleAndAppend extends JFrame {
public class MyJTextPane extends JTextPane {
/**
* Append some text to this pane.
* @param s
*/
public void append(String s) {
try {
Document doc = this.getDocument();
doc.insertString(doc.getLength(), s, null);
} catch(BadLocationException e) {
System.err.println(e);
}
}

/**
* Append some text and change line.
* @param s
*/
public void appendLine(String s) {
try {
Document doc = this.getDocument();
doc.insertString(doc.getLength(), s + System.lineSeparator(), null);
} catch(BadLocationException e) {
System.err.println(e);
}
}
}
public Test1_ChangeStyleAndAppend() {
begin();
}

private void begin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

MyJTextPane pane0 = new MyJTextPane();
pane0.appendLine("MyJTextPane using append() and then calling setText()");
pane0.appendLine("Second line. ");
pane0.appendLine("Third line");
pane0.setText(pane0.getText() + "At last" + System.lineSeparator());
pane0.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane0, BorderLayout.NORTH);

MyJTextPane pane = new MyJTextPane();
// changeLineSpacing(pane, 1.5f, false);
pane.appendLine("MyJTextPane calling appendLine()");
pane.appendLine("Second line. ");
pane.appendLine("Third line");
pane.appendLine("At last");
pane.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane, BorderLayout.CENTER);


JTextPane pane2 = new JTextPane();
pane2.setText("Normal JTextPane calling setText()");
pane2.setText(pane2.getText() + System.lineSeparator() + "Second line. ");
pane2.setText(pane2.getText() + System.lineSeparator() + "Third line");
pane2.setText(pane2.getText() + System.lineSeparator() + "At last");
pane2.setBorder(new EtchedBorder(EtchedBorder.RAISED));
add(pane2, BorderLayout.SOUTH);

pack();
setVisible(true);
}

/**
* Select all the text of a <code>JTextPane</code> first and then set the line spacing.
* @param pane the <code>JTextPane</code> to apply the change
* @param factor the factor of line spacing. For example, <code>1.0f</code>.
* @param replace whether the new <code>AttributeSet</code> should replace the old set. If set to <code>false</code>, will merge with the old one.
*/
public static void changeLineSpacing(JTextPane pane, float factor, boolean replace) {
pane.selectAll();
MutableAttributeSet set = new SimpleAttributeSet(pane.getParagraphAttributes());
StyleConstants.setLineSpacing(set, factor);
pane.setParagraphAttributes(set, replace);
pane.setCaretPosition(0); //scroll to the top.
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
Test1_ChangeStyleAndAppend frame = new Test1_ChangeStyleAndAppend();

}

});
}
}

结果是:

enter image description here

我使用Windows。

我有两个 TextPanes,一个是 JTextPane ,另一个是我的类扩展 JTextPane在此我定义了两种方便的方法,append()appendLine()在我的自定义类中,但我还没有覆盖 super getText()方法。

现在,当我调用appendLine()时然后调用setText()在我的类里面,getText()的召唤添加另一个\r两行之间,使其成为 \r\r\n并在两行之间添加额外的空行。这不是我想要的,我不想要的\r待补充。

我有一个 JTextArea 类型的变量为了更改行间距,我将其更改为 JTextPane因为JTextArea无法调整行距。但它发生在整个项目和它使用的所有时间pane.setText(pane.getText + newText) 。现在我必须搜索所有出现的 setText()并将其更改为 appendLine() 。在进行这个重大改变之前,我想了解为什么这个额外的 \r已添加。

如果我总是打电话append()或随时调用setText(pane.getText() + newText) ,它不会发生。但如果我打电话append()先调用setText(pane.getText() + newText) , \r已添加。

有人阐明这一点吗?

最佳答案

Windows 的行分隔符是“\r\n”。

文档仅使用“\n”作为新行字符串。

不确定到底是什么问题,但在 insertString(...) 和 getText() 方法之间处理换行符字符串的方式上存在不一致。

简单的解决方案是使用其换行符字符串更新文档,而不是平台换行符字符串。

//doc.insertString(doc.getLength(), s + System.lineSeparator(), null);
doc.insertString(doc.getLength(), s + "\n", null);

关于java - Swing - 我的类扩展了 JTextPane 并首先调用我的append(),然后调用 super.setText() 添加额外的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43911122/

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