gpt4 book ai didi

java - 换行属性 JEditorPane

转载 作者:行者123 更新时间:2023-11-29 07:12:01 26 4
gpt4 key购买 nike

谁知道如何更改 JEditorPane 的换行符属性?

我无法在我的 JTextPane 文本中找到换行符(它既不是 \n 也不是 \r),所以我可以' 正确计算此文本的行数。我想更改 \n 的换行符属性。

最佳答案

这个例子对我有用...

public class TestEditorPane {

public static void main(String[] args) {
new TestEditorPane();
}

public TestEditorPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new EditorPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class EditorPane extends JPanel {

private JEditorPane editor;

public EditorPane() {

setLayout(new GridBagLayout());
editor = new JEditorPane();
editor.setContentType("text/plain");

JButton button = new JButton("Dump");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = editor.getText();
String[] parts = text.split(System.getProperty("line.separator"));
// String[] parts = text.split("\n\r");
for (String part : parts) {
if (part.trim().length() > 0) {
System.out.println(part);
}
}
}
});

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(editor, gbc);

gbc.gridx = 0;
gbc.gridy++;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
add(button, gbc);
}
}
}

在windows下,换行符好像是/n/r。我也使用了系统属性 line.separator,它似乎对我有用。

关于java - 换行属性 JEditorPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059357/

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