我使用 2 个 JEditorPane 将文本从一个 Pane 传输到另一个 Pane 。
一旦我传输了数据,我就会执行以下操作:
JEditorPane.setText(null);
JEditorPane.setCaretPosition(0);
但正如您从附图中看到的那样,返回操作使提示出现在下方一行。我该如何解决这个问题?
编辑:您认为以下内容正确吗?如果是这样,那么为什么插入符号不将自己定位到字符 0 位置?
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent ke) {
int kc = ke.getKeyCode();
if (kc == ke.VK_ENTER) {
System.out.println(editorPaneHistory.getText());
System.out.println(editorPaneHomeText.getText());
editorPaneHistory.setText(editorPaneHomeText.getText());
//JEditorPane - editorPaneHistory
//JEditorPane - editorPaneHomeText
editorPaneHomeText.setText(null);
editorPaneHomeText.setCaretPosition(0);
}
}
}
最佳答案
代码运行后,JEditorPane 将通过插入换行符以通常的方式对 Enter 键使用react。尝试调用 ke.consume() 来“消耗”该事件,以便 JEditorPane 本身不会处理它。
关于Java JEditorPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2716256/