Java JEditorPane

发布于 2024-08-30 12:04:17 字数 986 浏览 1 评论 0原文

ChatGUI

我使用 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);

        }
    }
}

ChatGUI

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-09-06 12:04:17

代码运行后,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.

别低头,皇冠会掉 2024-09-06 12:04:17

不要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文