【问题标题】:Caret position in JTextPane is not correct ?! Bug or expected behavior?JTextPane 中的插入符号位置不正确?!错误或预期行为?
【发布时间】:2012-02-11 07:10:07
【问题描述】:

我偶然发现了以下问题:

我想在组件的插入符号位置读取 JTextComponent 文档中的字符。当我使用 JTextPane 时,在插入符号位置返回的字符不是正确的。更详细地说,返回的字符是插入符号的位置减去行号的字符!!! (插入符号位置 - 当前行的编号)。另一方面,当我使用 JTextArea 时,结果是正确的......为了证明这一点,我已经实现了一个示例程序,您可以使用它。

所以最大的问题是,在 JTextPane 的情况下如何获得插入符号位置的字符?

为什么JTextPane 不返回与JTextArea 相同的插入符号位置,以及为什么JTextPane 返回的字符不是我们在屏幕上可以看到的字符? 描述的行为是错误吗?

您可以在下面找到示例程序的代码以及非常有趣和意想不到的结果的屏幕截图

使用 JTextPane。 CARET 位置 17 中的字母是 e。不...

使用 JTextArea。这里我的插入符号位置与以前相同,但现在我得到插入符号位置 20 并且返回字母是 \r\n (与预期的一样)。

这是您可以用来查看这种奇怪行为的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;

public class Example extends JFrame {
//  use this instead of JTextPane to see the difference

//  JTextComponent testingArea = new JTextArea(5,10);  
    JTextComponent testingArea = new JTextPane();
JButton button = new JButton("test");
JTextComponent resultArea = new JTextField(20);


public Example() {
    initialise();
    testingArea.setText("line1\r\nline2\r\nline3\r\nline4");
}


private void initialise() {
    testingArea.setPreferredSize(new Dimension(100,100));
    setLayout(new FlowLayout());
    getContentPane().add(testingArea);
    getContentPane().add(new JLabel("answer"));
    getContentPane().add(resultArea);
    getContentPane().add(button);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try { 
                int caretPosition = testingArea.getCaretPosition();
                char result = testingArea.getText().charAt(caretPosition);
                resultArea.setText("Char at caretPosition " + caretPosition + " is " + result);
            }catch (Exception e2) {
                e2.printStackTrace();
                resultArea.setText("ERROR");
            }

        }
    });
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    final Example ex = new Example();
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            ex.pack();
            ex.setVisible(true);

        }
    });
}
}

感谢您的帮助!

PS 我正在使用 java 6。

【问题讨论】:

  • 感谢大家(alain.janinm,StanislavL)的宝贵回答!

标签: java swing jtextarea jtextpane caret


【解决方案1】:

使用

char result = testingArea.getDocument().getText(caretPosition,1).charAt(0);

而不是

char result = testingArea.getText().charAt(caretPosition);

【讨论】:

    【解决方案2】:

    我认为在 JTextPane 中 EOL 被计为一个字符(\n 我想),而在 JTextArea 中它被计为两个(\r\n)。

    Oracle 文档说:

    JEditorPane 类是 Swing 样式化文本组件的基础,并提供了一种机制,您可以通过该机制添加对自定义文本格式的支持。如果您想要无样式的文本,请改用文本区域。

    因此文本区域仅基于给定文本,因此所有条目字符都被计算在内。 JEdi​​torPane 使用了 StyledDocument,因此可以解释 EOL。

    【讨论】:

    • 你是对的;在 Windows CR+LF 字符与抽象行尾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    相关资源
    最近更新 更多