JTextArea 换行 +进入

发布于 2024-08-19 10:37:06 字数 1376 浏览 4 评论 0原文

我已向 JTextArea 字段添加了一个按键侦听器,但它的行为并不符合我的预期。

inputTextArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent k) {
  //If the return button is hit, only set to a new line if shift is also down.
  if(k.getKeyChar() == KeyEvent.VK_ENTER) {
   if(k.isShiftDown()) {
    inputTextArea.append(" \n");
   } else {
    //Send The Message...
    boolean cleanTextField = false;
    try {
     sendMessage(inputTextArea.getText());
     cleanTextField = true;
     msgScrollPane.setAutoscrolls(true);

     JScrollBar vbar = msgScrollPane.getVerticalScrollBar();
     if ((vbar.getValue() + vbar.getVisibleAmount()) == vbar.getMaximum()) {
      msgPane.setCaretPosition(msgDoc.getLength());
     }
    } catch (Exception ex) {
     ex.printStackTrace();
     cleanTextField = false;
    } finally {
     if(cleanTextField) {
      inputTextArea.setText("");
     }
    }
   }
  }
 }
});

我想要这个: - 如果按下返回按钮并且按下 Shift 键:添加新行。 - 如果按下返回按钮且未按下 Shift 按钮:不换行,但提交。

现在它的行为是这样的: - 如果我按下返回按钮并且 Shift 已按下:则不会添加任何行。什么也没发生。 - 如果我点击返回按钮并且 Shift 没有按下:已提交,但如果我再次开始输入,它将从新行开始。

有人知道如何做我想做的事吗?

编辑:

我尝试了一些其他代码来检测 Shift 按钮是否按下:

                    if((k.getModifiersEx() == KeyEvent.SHIFT_DOWN_MASK) || 
                            (k.getModifiers() == KeyEvent.SHIFT_DOWN_MASK)) {

这也不起作用

I've added a keylistener to my JTextArea field, but it doesn't behave as I expected.

inputTextArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent k) {
  //If the return button is hit, only set to a new line if shift is also down.
  if(k.getKeyChar() == KeyEvent.VK_ENTER) {
   if(k.isShiftDown()) {
    inputTextArea.append(" \n");
   } else {
    //Send The Message...
    boolean cleanTextField = false;
    try {
     sendMessage(inputTextArea.getText());
     cleanTextField = true;
     msgScrollPane.setAutoscrolls(true);

     JScrollBar vbar = msgScrollPane.getVerticalScrollBar();
     if ((vbar.getValue() + vbar.getVisibleAmount()) == vbar.getMaximum()) {
      msgPane.setCaretPosition(msgDoc.getLength());
     }
    } catch (Exception ex) {
     ex.printStackTrace();
     cleanTextField = false;
    } finally {
     if(cleanTextField) {
      inputTextArea.setText("");
     }
    }
   }
  }
 }
});

I want this:
- If the return button is hit and shift is down: add a new line.
- If the return button is hit and the shift button isn't down: no new line, but submit.

Now it behaves like this:
- If I hit the return button and shift is down: no line added. Nothing happens.
- If I hit the return button and shift isn't down: submitted, but if I start typing again it begins on new line.

Does someone know how to do what I want?

EDIT:

I tried some other code to detect if the shift button is down:

                    if((k.getModifiersEx() == KeyEvent.SHIFT_DOWN_MASK) || 
                            (k.getModifiers() == KeyEvent.SHIFT_DOWN_MASK)) {

This doesn't work as well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

另类 2024-08-26 10:37:06

您可以使用 JTextAreaInputMapActionMap 将按键映射到操作:

private static final String TEXT_SUBMIT = "text-submit";
private static final String INSERT_BREAK = "insert-break";
...
private void initialize() {
    InputMap input = inputTextArea.getInputMap();
    KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
    KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER");
    input.put(shiftEnter, INSERT_BREAK);  // input.get(enter)) = "insert-break"
    input.put(enter, TEXT_SUBMIT);

    ActionMap actions = inputTextArea.getActionMap();
    actions.put(TEXT_SUBMIT, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            submitText();
        }
    });
}
...
private void submitText() {
    // TODO
}  

ENTER - “insert-break” - 用于 shift ENTER

You may use the InputMap and ActionMap of the JTextArea to map the key strokes to actions:

private static final String TEXT_SUBMIT = "text-submit";
private static final String INSERT_BREAK = "insert-break";
...
private void initialize() {
    InputMap input = inputTextArea.getInputMap();
    KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
    KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER");
    input.put(shiftEnter, INSERT_BREAK);  // input.get(enter)) = "insert-break"
    input.put(enter, TEXT_SUBMIT);

    ActionMap actions = inputTextArea.getActionMap();
    actions.put(TEXT_SUBMIT, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            submitText();
        }
    });
}
...
private void submitText() {
    // TODO
}  

The original action for ENTER - "insert-break" - is used for shift ENTER.

情释 2024-08-26 10:37:06

尝试使用 keyTyped 而不是 keyPressed。我相信 keyPressed 为您提供了一个用于 Shift 和 Enter 的事件,而 keyTyped 为您提供了一个带有修饰符的组合事件。

Try using keyTyped and not keyPressed. I beleive keyPressed gives you an event for the shift and for the enter, whereas keyTyped gives you one combined event with a modifier.

如若梦似彩虹 2024-08-26 10:37:06

不要在接收到事件后立即执行操作,而是通过使用 SwingUtilities.invokeLater() 发布它们来对它们进行排序以供稍后使用。代码应该如下所示:

if(k.isShiftDown()) {
   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
         inputTextArea.append(" \n");
      }
   });
} else {
   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
          //rest of the else body here
      }
   });

}

在我看来,这里出现的问题是因为应用程序定义的操作和内部操作没有正确排序,导致在修改文本之前发生重绘。

Instead of doing the actions immediately on receiving the event, sequence them for later by posting them using SwingUtilities.invokeLater(). The code should look like:

if(k.isShiftDown()) {
   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
         inputTextArea.append(" \n");
      }
   });
} else {
   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
          //rest of the else body here
      }
   });

}

In my opinion, the problems seen here are because application-defined actions and internal actions are not being properly sequenced, leading to repaints happening before the text has been modified.

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