JavaFX:如何使输入键提交 TextArea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25252558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX: How to make enter key submit TextArea
提问by Bassinator
Sorry if this seems a little too easy, I'm brand new to JavaFX, this is my first little app built with it.
对不起,如果这看起来有点太简单了,我是 JavaFX 的新手,这是我用它构建的第一个小应用程序。
I am trying to make a bare bones chat client. I am using the JavaFX Scene builder to make the client UI, and a controller class connected to the FXML.
我正在尝试制作一个简单的聊天客户端。我正在使用 JavaFX 场景构建器来制作客户端 UI,以及连接到 FXML 的控制器类。
How can I make is so that the current text of in the text area is submitted to the server and the text area is cleared upon the enter key press, instead of using some kind of "send" button?
我怎样才能使文本区域中的当前文本提交到服务器并在按下回车键时清除文本区域,而不是使用某种“发送”按钮?
EDIT: Here is the code that is not working:
编辑:这是不起作用的代码:
//...
public class FXMLDocumentController
{
//...
@FXML private TextArea messageBox;
//...
messageBox.setOnKeyPressed(new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent keyEvent)
{
if(keyEvent.getCode() == KeyCode.ENTER)
{
//sendMessage();
}
}
});
//...
采纳答案by Ryan J
This should get you what you want:
这应该让你得到你想要的:
TextArea area;
//... (initialize all your JavaFX objects here...)
// wherever you assign event handlers...
area.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
String text = area.getText();
// do your thing...
// clear text
area.setText("");
}
}
});
I might add, that if you are so inclined to provide botha button and an enter key event, you could tie the event handler functions of both controls to a single common function in a way such as this:
我想补充的是,如果你愿意的话,提供两个按钮和输入键时,你可以在某种程度上像这样把两个控件的事件处理函数到一个共同的功能:
Button sendButton;
TextArea area;
// init...
// set handlers
sendButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
sendFunction();
}
});
area.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
sendFunction();
}
}
});
// define send function
public void sendFunction() {
String text = this.area.getText();
// do the send stuff
// clear text (you may or may not want to do this here)
this.area.setText("");
}
Either way works, good luck.
无论哪种方式都有效,祝你好运。
回答by Mr_Lucky
You can use lambda expressions also ... I think it is more elegant and simply
你也可以使用 lambda 表达式......我认为它更优雅和简单
textArea.setOnKeyPressed(event -> {
if(event.getCode() == KeyCode.ENTER){
//type here what you want
}
});
回答by just some guy
In addition to the other answers, I think it might be useful in some applications to not actually invoke the send function if the user pressed SHIFT+ENTER. In that case he/she maybe actually wanted a new line.
除了其他答案之外,我认为在某些应用程序中,如果用户按下 SHIFT+ENTER,则不实际调用发送函数可能很有用。在这种情况下,他/她可能实际上想要一条新线路。
textArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume(); // otherwise a new line will be added to the textArea after the sendFunction() call
if (event.isShiftDown()) {
textArea.appendText(System.getProperty("line.separator"));
} else {
sendFunction();
}
}
});
If you don't want to send empty messages you can do something like this:
如果您不想发送空消息,您可以执行以下操作:
textArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume();
if (event.isShiftDown()) {
textArea.appendText(System.getProperty("line.separator"));
} else {
if(!textArea.getText().isEmpty()){
sendFunction();
}
}
}
});