Java JEditorPane
我使用 2 JEditorPane 将文本从一个 JEditorPane 传输到另一个。
一旦我传输了数据,我就会执行以下操作:
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);
}
}
}
im using 2 JEditorPane to transfer text from one to another.
once i have transfered the data i do the following:
JEditorPane.setText(null);
JEditorPane.setCaretPosition(0);
but as you can see from the attached image the return action makes the prompt appear a row down. how can i fix this?
EDIT: does the following seem correct to you? if so then why is caret not positioning itself to chracter 0 position?
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码运行后,JEditorPane 将通过插入换行符以通常的方式对 Enter 键做出反应。尝试调用 ke.consume() 来“消耗”事件,这样 JEditorPane 本身就不会处理它。
After your code runs, the JEditorPane is reacting to the enter key in the usual way, by inserting a newline. Try calling
ke.consume()
to "consume" the event so that the JEditorPane itself doesn't handle it.不要使用 KeyListener。您应该使用自定义操作。这样就可以替换默认的Action了。阅读按键绑定。
Don't use a KeyListener. You should be using a custom Action. This way you can replace the default Action. Read up on Key Bindings.