gpt4 book ai didi

java - 使用回车键传递字段?

转载 作者:行者123 更新时间:2023-12-02 06:16:40 24 4
gpt4 key购买 nike

我正在寻找一种在 VerticalLayout 或其他布局中使用 Enter 键传递字段的方法。在 vaadin 书中有一个带有快捷方式和处理程序监听器的示例,但我不知道如何实现它。

我正在尝试这个。

public class MyWindow extends Window implements Handler{
private Action action_enter; //pass fields with enter
private Action action_esc;
private TextField name, lastName;

public MyWindow(){
super("this window is opened");
VerticalLayout vLayout = new VerticalLayout();
setContent(vLayout);

center();
setModal(true);
setClosable(false);
setDraggable(false);
setResizable(false);

//actions
action_enter = new ShortcutAction("Enter key", ShortcutAction.KeyCode.ENTER, null);
action_esc = new ShortcutAction("Esc key", ShortcutAction.KeyCode.ESCAPE, null);
addActionHandler(this);

//fields
name = new TextField("Name");
lastName = new TextField("Last name");

name.focus();
vLayout.addComponent(name);
vLayout.addComponent(lastName);
}


@Override
public Action[] getActions(Object target, Object sender) {
return new Action[] { action_enter, action_esc };
}

@Override
public void handleAction(Action action, Object sender, Object target) {

/** close window with esc key */
if(action == action_esc){
close();
}

/** pass fields with enter key */
if(action == action_enter){
//here pass fields with enter key
}
}

}

有什么想法吗?

最佳答案

使用 ShortcutListener 尝试这种方式:

    ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

@Override
public void handleAction(Object sender, Object target) {

if (target instanceof VerticalLayout) { // VerticalLayout or other
// sending fileds here
}
}
};

addShortcutListener(skEnterListener);

使用 Enter 而不是 Tab 更改 TextField 的焦点:

    final TextField tf1 = new TextField("tf1");
tf1.setId("tf1");

final TextField tf2 = new TextField("tf2");
tf2.setId("tf2");


ShortcutListener skEnterListener = new ShortcutListener("Enter", ShortcutAction.KeyCode.ENTER, null){

@Override
public void handleAction(Object sender, Object target) {

if (target instanceof TextField) {

TextField field = (TextField) target;

if ("tf1".equals(field.getId())) {
tf2.focus();
}

if ("tf2".equals(field.getId())) {
tf1.focus();
}
}
}
};

addShortcutListener(skEnterListener);

关于java - 使用回车键传递字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21387842/

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