gpt4 book ai didi

java - 如何在 Java JFrame GUI 中使用 requestFocus?

转载 作者:行者123 更新时间:2023-12-01 17:37:21 25 4
gpt4 key购买 nike

我收到了一项作业,但我对 Java 完全陌生(我已经使用 C++ 和 Python 编程两年了)。

所以我们正在做 GUI,基本上我们扩展了 JFrame 并添加了几个字段。

假设我们有一个名为“Text 1”和“Text 2”的字段。当用户在文本 1 中按下 Enter 键并将光标移至文本 2 时,我尝试添加

private JTextField textfield1() {

textfield1 = new JTextField();
textfield1.setPreferredSize(new Dimension(200, 20));

textfield1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {

textfield1text = textfield1.getText().trim();
textfield1.setText(textfield1text);
System.out.println(textfield1text);

textfield1.requestFocus();
}
});

return textfield1;
}

但这根本不起作用。

我注意到不推荐使用 requestFocus,而应该使用 requestFocusWindows。但我也尝试过。根据一些阅读,我似乎必须执行键盘操作和监听器?但我的老师说它只需要 1 行...

最佳答案

嗯,您有 textfield1.requestFocus(),但您的描述暗示您需要 textfield2.requestFocus()。 (即2)。

关于java - 如何在 Java JFrame GUI 中使用 requestFocus?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4964990/

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