找出在 jTextPane 中的某个位置是否有特定的字符 [java]
Find out if at a certain position in a jTextPane there is a specific char [java]
我正在为 jTextPane 的 getActionMap 编写一个动作,当我键入 ')' 时,如果在插入符位置有一个 ')',该动作将覆盖符号,否则它通常键入 ')'。
这是代码:
Action action1 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
char bracket = ')';
try {
int position = jTextPane1.getCaretPosition();
if (jTextPane1.getDocument.getText(position,1)== ")") {
jTextPane1.getDocument().remove(position, 1);
jTextPane1.getDocument().insertString(position, ")", null);
} else {
jTextPane1.getDocument().insertString(position, ")", null);
}
} catch (Exception e1) {}
}
};
String key1 = "typed )";
jTextPane1.getInputMap().put(KeyStroke.getKeyStroke(key1), key1);
jTextPane1.getActionMap().put(key1, action1);
}
我不明白为什么,即使 'if' 的布尔值是真,它也不会进入 if 方式。你能帮帮我吗?
所以,这是一个 DocumentFilter
的简单示例,当用户尝试添加 )
字符时,过滤器会检查它之前的字符是否也是 )
并且基本上拒绝更新
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextPane tp = new JTextPane();
add(new JScrollPane(tp));
((AbstractDocument) tp.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
super.insertString(fb, offset, string, attr);
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
System.out.println("Remove");
super.remove(fb, offset, length);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (offset > 0) {
String before = fb.getDocument().getText(offset - 1, 1);
if (!(")".equals(before) && ")".equals(text))) {
super.replace(fb, offset, length, text, attrs);
}
} else {
super.replace(fb, offset, length, text, attrs);
}
}
});
}
}
虽然我可以让它在插入的字符是 (
时添加 )
,但它将插入符号移到了 )
之后,这并不是特别有用
even if the the boolean of the 'if' is true, it does not go in the if way
您是否进行了任何基本调试以查看发生了什么?
if (jTextPane1.getDocument.getText(position,1)== ")") {
问题是您不应该使用“==”进行对象比较。这永远不会是真的。
您应该使用 equals(...)
方法:
这很容易通过简化代码和添加调试语句来验证。
String text = jTextPane1.getDocument().getText(position, 1);
System.out.println(text);
if (text.equals(")"))
{
System.out.println("matches");
}
else
{
System.out.println("doesn't match");
}
使用上面的简单代码,您可以轻松验证文本值是否符合您的预期。不要用多种方法编写复杂的语句。将代码简单化为多条语句使得调试更容易。
我正在为 jTextPane 的 getActionMap 编写一个动作,当我键入 ')' 时,如果在插入符位置有一个 ')',该动作将覆盖符号,否则它通常键入 ')'。 这是代码:
Action action1 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
char bracket = ')';
try {
int position = jTextPane1.getCaretPosition();
if (jTextPane1.getDocument.getText(position,1)== ")") {
jTextPane1.getDocument().remove(position, 1);
jTextPane1.getDocument().insertString(position, ")", null);
} else {
jTextPane1.getDocument().insertString(position, ")", null);
}
} catch (Exception e1) {}
}
};
String key1 = "typed )";
jTextPane1.getInputMap().put(KeyStroke.getKeyStroke(key1), key1);
jTextPane1.getActionMap().put(key1, action1);
}
我不明白为什么,即使 'if' 的布尔值是真,它也不会进入 if 方式。你能帮帮我吗?
所以,这是一个 DocumentFilter
的简单示例,当用户尝试添加 )
字符时,过滤器会检查它之前的字符是否也是 )
并且基本上拒绝更新
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextPane tp = new JTextPane();
add(new JScrollPane(tp));
((AbstractDocument) tp.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
super.insertString(fb, offset, string, attr);
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
System.out.println("Remove");
super.remove(fb, offset, length);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (offset > 0) {
String before = fb.getDocument().getText(offset - 1, 1);
if (!(")".equals(before) && ")".equals(text))) {
super.replace(fb, offset, length, text, attrs);
}
} else {
super.replace(fb, offset, length, text, attrs);
}
}
});
}
}
虽然我可以让它在插入的字符是 (
时添加 )
,但它将插入符号移到了 )
之后,这并不是特别有用
even if the the boolean of the 'if' is true, it does not go in the if way
您是否进行了任何基本调试以查看发生了什么?
if (jTextPane1.getDocument.getText(position,1)== ")") {
问题是您不应该使用“==”进行对象比较。这永远不会是真的。
您应该使用 equals(...)
方法:
这很容易通过简化代码和添加调试语句来验证。
String text = jTextPane1.getDocument().getText(position, 1);
System.out.println(text);
if (text.equals(")"))
{
System.out.println("matches");
}
else
{
System.out.println("doesn't match");
}
使用上面的简单代码,您可以轻松验证文本值是否符合您的预期。不要用多种方法编写复杂的语句。将代码简单化为多条语句使得调试更容易。