English 中文(简体)
JTextPane的护理站不正确! Bug或预期行为?
原标题:Caret position in JTextPane is not correct ?! Bug or expected behavior?

i) 陷入以下问题:

我想读一下该构成部分所在位置的JTextComponent文件的性质。 当有人使用JTextPane时,在护理站返回的特性是不正确的。 更详细来说,归还的特性是护理者的地位减去线数! (现线的留级数)。 另一方面,如果使用JTextArea,结果是正确的。 为了证明这一点,我已经实施了你可以采用的样本方案。

因此,大问题是,在JTextPane一案中,如何获得护理职位的性质?

Why JTextPane does not returns the same caret position as JTextArea, and further more why the character returned by JTextPane is not the one that we can see in the screen? Is the described behavior a bug?

下面你可以找到样本方案的代码,并筛选出非常有趣和意想不到的结果。

www.un.org/Depts/DGACM/index_spanish.htm 利用JTextPane. 第17号行政指示中的文字是......。

alt text

Usign a JTextArea. Here i have the caret in the same position as before, but now i get caret position 20 and the return letter is (with is the one expected).

alt text

这里,你可以用来看到这种奇怪的行为:

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
line2
line3
line4");
}


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 i am using java 6.

最佳回答

用途

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

而不是

char result = testingArea.getText().charAt(caretPosition);
问题回答

I think in JTextPane EOL is counted as one character ( I suppose) whereas in JTextArea it s counted as two ( ).

甲骨质文件说:

<><>>>> JEditor 板门类是浏览风格的文字成分的基础,它提供了一个机制,使你能够增加对习惯文本格式的支持。 如果你想要不划一的案文,则使用一个文本领域。

因此,案文区仅以特定案文为基础,因此所有切入点都算在内。 JEditorPane 采用的方法 文件可解释EOL。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签