找出jTextPane中某个位置是否有特定的char[java]
我正在为jTextPane的getActionMap编写一个操作,当我键入“')”时,如果在插入符号位置有“'),该操作将覆盖符号,否则它通常键入“')”。 代码如下:找出jTextPane中某个位置是否有特定的char[java],java,swing,jtextpane,jdk1.6,Java,Swing,Jtextpane,Jdk1.6,我正在为jTextPane的getActionMap编写一个操作,当我键入“')”时,如果在插入符号位置有“'),该操作将覆盖符号,否则它通常键入“')”。 代码如下: Action action1 = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { char bracke
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的方式进行。你能帮我吗?这是一个简单的
文档过滤器的例子,当用户试图添加)
字符时,过滤器会检查前面的字符是否也是)
,并基本上拒绝更新
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);
}
}
});
}
}
虽然我可以让它在插入的字符是(
)时添加)
,但它将插入符号移动到)
之后,这并没有特别的帮助,因此,这是文档过滤器的一个简单示例,当用户尝试添加)
字符时,过滤器检查前面的字符是否也是)
,并基本上拒绝更新
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);
}
}
});
}
}
当插入的字符是(
)时,我可以让它添加)
,但它将插入符号移动到)
之后,这并没有特别的帮助
即使“if”的布尔值为true,它也不会以if的方式进行
您是否进行了任何基本调试以查看发生了什么
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");
}
通过上面的简单代码,您可以轻松地验证文本值是否符合预期。不要用多种方法编写复杂的语句。将代码简化为多个语句使调试更加容易
即使“if”的布尔值为true,它也不会以if的方式进行
您是否进行了任何基本调试以查看发生了什么
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");
}
通过上面的简单代码,您可以轻松地验证文本值是否符合预期。不要用多种方法编写复杂的语句。将代码简化为多个语句可以使调试更加容易。您是否考虑过使用文档过滤器
?不,我没有。我认为使用击键对这类任务很有好处。如何在此处使用DocumentFilter?jTextPane1.toString()
?我很惊讶你还没有遇到索引越界异常。我想您是想使用jTextPane1.getText()
如果OP将文本粘贴到组件中会发生什么?不会通知密钥绑定。DocumentFilter
允许您在文档实际提交(并更改)之前,处理文档“即将”发生的更改。我编辑了问题。我将jTextPane1.toString()更改为jTextPane1.getDocument().getText(位置,1),但仍然不起作用。您是否考虑过使用文档过滤器
?不,我没有。我认为使用击键对这类任务很有好处。如何在此处使用DocumentFilter?jTextPane1.toString()
?我很惊讶你还没有遇到索引越界异常。我想您是想使用jTextPane1.getText()
如果OP将文本粘贴到组件中会发生什么?不会通知密钥绑定。DocumentFilter
允许您在文档实际提交(并更改)之前,处理文档“即将”发生的更改。我编辑了问题。我用jTextPane1.getDocument().getText(位置1)更改了jTextPane1.toString(),但仍然不起作用。