Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextPane中的插入符号位置不正确?!Bug还是预期的行为?_Java_Swing_Jtextarea_Jtextpane_Caret - Fatal编程技术网

Java JTextPane中的插入符号位置不正确?!Bug还是预期的行为?

Java JTextPane中的插入符号位置不正确?!Bug还是预期的行为?,java,swing,jtextarea,jtextpane,caret,Java,Swing,Jtextarea,Jtextpane,Caret,我遇到了以下问题: 我想读取JTextComponent文档中位于组件插入符号位置的字符。使用JTextPane时,在插入符号位置返回的字符不正确。更详细地说,返回的字符是插入符号的位置减去行号!!!(插入符号位置-当前行的编号)。另一方面,当我使用JTextArea时,结果是正确的。。。为了演示这一点,我实现了一个示例程序,您可以使用它 所以最大的问题是,在JTextPane的情况下,如何获得插入符号位置的字符 为什么JTextPane不返回与JTextArea相同的插入符号位置,以及为什么J

我遇到了以下问题:

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

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

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

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

使用JTextPane。插入符号位置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我正在使用Java6。

使用

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

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

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

Oracle文档说明:

强>类是JavaScript样式文本组件的基础,并提供了一种机制,通过该机制可以添加对自定义文本格式的支持。如果需要未设置样式的文本,请使用文本区域。


所以文本区域只基于给定的文本,所以所有的输入字符都是计数的。JEditorPane使用了StyledDocument,因此可以解释EOL。

你说得对;关于Windows CR+LF字符与抽象终端的对比。感谢所有人(alain.janinm,StanislavL)提供了宝贵的答案!