gpt4 book ai didi

java - 除非先单击 JButton,否则 keyPressed 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:10 24 4
gpt4 key购买 nike

我创建了一个使用 JButtons 来增加数字的小应用程序。这些按钮不应该是可单击的,而是可以通过键盘激活(即文本字段中的数字随着键盘的按下而增加,而不是通过使用鼠标单击按钮来增加)。我的问题是,当应用程序首次启动时,键盘不会执行任何操作,直到我第一次单击其中一个按钮 - 即使单击该按钮不会进行任何操作。我试图将其中一个按钮 requestFocusInWindow() 认为如果它已经被聚焦,那么按键就会起作用,但无论我将它放在我的主方法还是 Controller 类中,这似乎都不起作用。我试图弄清楚我是否需​​要执行 KeyboardFocusManageraddFocusListener() (但我不希望在按钮获得/失去焦点时总是发生某些事情)。我已经尝试了很多事情,我的头在旋转,试图将框架或 Controller 类添加到我的主要方法中。以下是我当前的代码:

带有 Main 的类

import javax.swing.JFrame;

public class Count {

public static void main(String[] args) {
CountController frame = new CountController();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(560, 150);
frame.setVisible(true);
//I've tried to add the button and requestFocusInWindow here
//as well as tried a frame.addWindowFocusListener
}
} // end class

Controller 类

imports ...
public class CountController extends JFrame implements KeyListener {
private JLabel ...
private JTextField ...
private JButton ....
int ...
// no-argument constructor
public CountController() {
super("Title");
setLayout(null); // position GUI components explicitly

//set up JLabels in following manner
label = new JLabel("some label");
label.setBounds(47, 5, 45, 25);
label.setHorizontalAlignment(JLabel.CENTER);
add(label);

//set up JTextFields in following manner
textField = new JTextField("0");
textField.setBounds(47, 30, 45, 25);
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setBackground(Color.WHITE);
textField.setEditable(false);
add(textField);

//set up JButtons in the following manner
button = new JButton("some text");
button.setBounds(15, 70, 110, 25);
button.setBackground(Color.WHITE);
add(button);
button.addKeyListener(this);
//I've tried adding requestFocusInWindow() here as well
} // end constructor

//begin KeyListener stuff
@Override
public void keyPressed(KeyEvent event){
int keyCode = event.getKeyCode();
switch(keyCode){
case #: //# is ASCII #
do some things;
call a method();
break;
}
}
@Override
public void keyReleased(KeyEvent event){
button.setBackground(Color.WHITE);
}
@Override
public void keyTyped(KeyEvent event){
// nothing but this is needed for implementing KeyListener
}

//List of methods that are called from switch
...

//I've tried to add a public void initialize(){}

}//end CountController class

如果您能提供任何有关使其正常工作的意见,我将不胜感激,这样我就不必在按键工作之前先单击按钮。谢谢!

最佳答案

为了让监听器触发事件​​,其注册的组件必须首先获得焦点并拥有焦点。使用key bindings API相反。

以下内容使用 JComponent.WHEN_IN_FOCUSED_WINDOW 定义将生成按键事件的上下文。在此配置中,无论焦点是什么,只要窗口当前处于焦点状态,事件仍然会生成

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class Count {

public static void main(String[] args) {
CountController frame = new CountController();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(560, 150);
frame.setVisible(true);
//I've tried to add the button and requestFocusInWindow here
//as well as tried a frame.addWindowFocusListener
}

public static class CountController extends JFrame {

// no-argument constructor
public CountController() {
super("Title");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

//set up JLabels in following manner
JLabel label = new JLabel("some label");
label.setHorizontalAlignment(JLabel.CENTER);
add(label, gbc);

//set up JTextFields in following manner
JTextField textField = new JTextField("0", 20);
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setBackground(Color.WHITE);
textField.setEditable(false);
add(textField, gbc);

//set up JButtons in the following manner
JButton button = new JButton("some text");
add(button, gbc);

InputMap im = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = button.getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_3, KeyEvent.SHIFT_DOWN_MASK), "Pressed.#");
am.put("Pressed.#", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "#");
}
});

} // end constructor

}//end CountController class
} // end class

虽然我会注册针对父容器的绑定(bind)而不是按钮,但这只是我的想法。

并且,不使用 null 布局的原因...这就是您的原始代码在我的电脑上的样子

Reasons for not using null layouts

关于java - 除非先单击 JButton,否则 keyPressed 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53720471/

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