gpt4 book ai didi

java - 按下回车键,提交输入并移动到另一个项目

转载 作者:行者123 更新时间:2023-11-30 10:28:40 25 4
gpt4 key购买 nike

按下回车键,如何提交输入并移动到另一个项目,例如TextField, DatePicker, Button 等?

最佳答案

只需使用 onAction 事件处理程序并调用 requestFocus(...) 即可在用户提交操作时聚焦下一个控件:

@FXML TextField fName;
@FXML TextField lName;
@FXML DatePicker birthDay;
@FXML TextField address;
@FXML TextField city;
@FXML TextField telephoneNmbr;
@FXML TextField email;
@FXML Button closeBtn;
@FXML Button okBtn;

public void initialize(){

fName.setOnAction(e -> lName.requestFocus());
lName.setOnAction(e -> birthDay.requestFocus());
birthDay.setOnAction(e -> address.requestFocus());

// etc...

okBtn.setOnAction(e -> {
// whatever you need to do when OK is pressed...
});
}

如果需要,您可以减少代码量:

Control[] focusOrder = new Control[] {
fName, lName, birthDay, address, city,
telephoneNmbr, email, closeBtn, okBtn};

for (int i = 0 ; i < focusOrder-1 ; i++) {
Control nextControl = focusOrder[i+1];
focusOrder[i].addEventHandler(ActionEvent.ACTION, e -> nextControl.requestFocus());
}

okBtn.setOnAction(...);

请注意,如果按钮具有键盘焦点,空格键通常与触发按钮上的操作相关联。您可以将该按钮设置为“默认按钮”,这将更改其外观并使其响应(半全局)回车键。

关于java - 按下回车键,提交输入并移动到另一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com