简体   繁体   English

Java Swing JTextArea镜像

[英]Java Swing JTextArea mirroring

I have made a user interface which has two JTextAreas. 我制作了一个具有两个JTextAreas的用户界面。 I would like one text area to recieve direct input from the user, and I would then essentially like the other one to mirror it exactly. 我希望一个文本区域能够接收到来自用户的直接输入,然后从本质上希望另一个文本区域能够准确地反映出该输入。 So this means as well as having the exact same characters added to it, it also correctly matches backspaces and return key presses etc. I have attempted to achieve this by emulating backspace and return keypresses using the Robot(int keycode) function, giving it the right keycode 10 being Enter and 8 being Backspace. 因此,这意味着除了添加完全相同的字符外,还正确地匹配了退格键和返回键等。我试图通过使用Robot(int keycode)函数模拟退格键并返回键来实现此目的,右键码10为Enter键,而8为Backspace键。 If it is a regular character then I simply append it to the second JTextArea. 如果它是常规字符,那么我只需将其附加到第二个JTextArea。 This works fine for the characters and return key presses, but pressing Backspace just draws the unknown character rectangle to the mirroring text area. 这对于字符和按回车键效果很好,但是按Backspace只会将未知字符矩形绘制到镜像文本区域。

在此处输入图片说明

Ideally I would like to be able to directly pass the events which occur in the input JTextArea to the ouput JTextArea rather than manually working out the appropriate behavior myself. 理想情况下,我希望能够将输入JTextArea中发生的事件直接传递给输出JTextArea,而不是自己亲自制定适当的行为。

This is the part of the program which creates the input JTextArea and assigns a keylistener: 这是程序的一部分,该程序创建输入JTextArea并分配键侦听器:

inputBox = new JTextArea();
    inputBoxScroller = new JScrollPane();
    inputBoxScroller.setViewportView(inputBox);
    inputBox.addKeyListener(this);

This is the part of the program which recieves the keylisten events and works out what to do based on the keycode of the keys pressed, which I would like to be changed to somehow 'forward' the keylisten event to the output JTextArea, as this would be more ideal. 这是程序的一部分,该程序接收keylisten事件并根据所按下的键的键码确定要执行的操作,我想将其更改为以某种方式将keylisten事件“转发”到输出JTextArea,因为这会更理想。

public void keyTyped(KeyEvent keyevent) {
    String keyPressed = String.valueOf(keyevent.getKeyChar());
    int keyCode = keyevent.getKeyCode();

    if(keyCode == 10) {
        try {
            System.out.println(keyevent.toString());
            autoKeypresser = new Robot();
            outputBox.requestFocus();
            autoKeypresser.keyPress(10);
            inputBox.requestFocus();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    } 

    if(keyCode == 8) {
        try {
            autoKeypresser = new Robot();
            outputBox.requestFocus();
            autoKeypresser.keyPress(8);
            inputBox.requestFocus();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    } else {
        outputBox.append(keyPressed);
    }
}

I would then essentially like the other one to mirror it exactly 然后,我基本上希望另一个人准确地反映它

All you need to do is share the model: 您需要做的就是共享模型:

JTextArea textArea1 = new JTextArea(...);
JTextArea textArea2 = new JTextArea();
textArea2.setDocument( textArea1.getDocument() );

Now whatever you type in either text area will be reflected in both text areas. 现在,您在任一文本区域中键入的内容都会在两个文本区域中反映出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM