- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个使用 JButtons 来增加数字的小应用程序。这些按钮不应该是可单击的,而是可以通过键盘激活(即文本字段中的数字随着键盘的按下而增加,而不是通过使用鼠标单击按钮来增加)。我的问题是,当应用程序首次启动时,键盘不会执行任何操作,直到我第一次单击其中一个按钮 - 即使单击该按钮不会进行任何操作。我试图将其中一个按钮 requestFocusInWindow()
认为如果它已经被聚焦,那么按键就会起作用,但无论我将它放在我的主方法还是 Controller 类中,这似乎都不起作用。我试图弄清楚我是否需要执行 KeyboardFocusManager
或 addFocusListener()
(但我不希望在按钮获得/失去焦点时总是发生某些事情)。我已经尝试了很多事情,我的头在旋转,试图将框架或 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
布局的原因...这就是您的原始代码在我的电脑上的样子
关于java - 除非先单击 JButton,否则 keyPressed 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53720471/
这是我的代码: $( '#Example' ).on( "keypress", function( keyEvent ) { if ( keyEvent.which != 44 ) {
自从我切换到 MAC 以来,我一直讨厌这样一个事实,即我必须使用 cmd+tab 的 alt 键来隐藏窗口。 我知道有一些应用程序(如 witch )可以替代 cmd+tab 功能,但我喜欢当前的界面
我希望在文本框上按键时运行一个函数,因此我有以下代码: $("input[x]").keypress(function() { DoX(); }) 这工作正常,但在我的函数中我
我有一个场景,我想通过进行 Http REST 调用来获取匹配的字符串,将每次击键时输入框中出现的字符串发送到服务器。但它没有按我的预期工作,例如,假设输入框中的当前值为“modul”,如果我在“mo
我刚刚开始使用 p5.js,我喜欢它的简单性,但有一点我无法掌握。 我设置了以下Fiddle : function Player(location, width, height, speed, wei
这里有两个类: public class Cls implements Runnable, KeyListener Thread t; Object obj; public Cls(Thing obj
我将一些程序从学校的计算机(Mac)传输到我的家用电脑。进入我的计算机后,我注意到按键现在在每个程序中都不起作用。我花了几个小时试图找出 KeyPressed 不起作用的原因。两台电脑都使用Eclip
我正在用java为自己制作一个俄罗斯方 block 克隆,作为一个学习项目。然而,我现在陷入了获取左右移动棋子的输入部分。我不确定问题是否出在我的方法中,或者问题出在未调用的 keyPressed 方
基本上我必须为一个项目使用 Processing(并非出于选择)并且遇到了关于一次按下多个键的问题。在 keyPressed() 函数中,我有多个条件,每个都将一个键映射到一个 Action 。这一切
目前我正在使用 jQuery,尤其是该函数 keypress() 这个函数可以告诉我很多关于按下哪个键、在哪里以及何时按下的信息。但什么时候是我的问题:所以这段代码... $(document).ke
我是 JavaScript 新手,在使用“keypress”事件时遇到问题;我目前正在做一个小的跳跃脚本测试,“keydown”和“keyup”事件都起作用,但“keypress”不起作用(我尝试在事
我试图在每次字段更改时执行一个函数。首先,我使用一个简单的 textarea 和一个 div: $("#stuff").keypress(function () { $(
嘿伙计们,可能是一个简单的问题,但在网上找不到任何东西。 我有一个包含搜索结果的列表,我希望能够使用向上和向下键在列表中导航。 if (e.keyCode == 40) { //down
我当前正在通过 .click 事件添加输入,然后想要监听此输入上发生的任何按键。但是,附加内容在插入后不会触发任何事件(即模糊、按键、焦点)。有没有人有什么建议?提前致谢! $("#recipient
我想知道是否可以在表单级别当表单中有控件时处理 KeyPress 事件。 当窗体上没有控件时我可以实现这一点,但是当我添加一些东西时,比如按钮,窗体失去焦点并且我无法将其返回,即使使用 Me.Focu
情况:表单有一个文本框,用户必须在其中键入一些文本。此文本框有一个 KeyPress 事件,在该事件中,窗体的文本属性更改为用户在每次击键时键入的文本。 这是文本框按键事件的代码: private
我正在开发一个应用程序并卡住了。我有一个普通的简单按钮。我想要做的是,如果我单击此按钮,它的行为就像您在软键盘上按空格键一样。 我该怎么做。一定有办法,因为您可以开发软键盘。 所以重复 xP 如果我按
在这个页面上: https://subnetipv4.com/ 如果您单击“IP 地址”列中的任何输入框,然后按“.”或“/”键(句点或斜杠),它会跳转到下一个输入框。 或者至少,它在桌面浏览器上是这
这段代码构建正确,一切似乎都有效,但 key 什么也没做。我认为要么是 Action 监听器,要么是椭圆没有更新。我正在尝试通过初学者 java 游戏编程。我确信这很容易,但我没有捕获它。如果这有所作
我在 jTable 中有一系列文本框和组合框。我使用框中的输入来过滤 JPQL 查询的结果,这些结果显示在 jTable 上。现在,想法是使该过程自动化,以便每次用户在框中键入一个字符时,应用程序都会
我是一名优秀的程序员,十分优秀!