java - 检测在JTextField中输入press
是否有可能在java中输入JTextField时检测到有人按Enter键? 无需创建按钮并将其设置为默认值。
9个解决方案
159 votes
Action被设计为使用ActionListener,就像Action一样。 参见Action的Action方法。
例如:
Action action = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("some action");
}
};
JTextField textField = new JTextField(10);
textField.addActionListener( action );
现在,使用Enter键时会触发该事件。
此外,即使您不想将按钮设置为默认按钮,您还可以使用按钮共享监听器。
JButton button = new JButton("Do Something");
button.addActionListener( action );
注意,此示例使用Action,其实现ActionListener,因为Action是具有附加功能的较新API。 例如,您可以禁用Action,这将禁用文本字段和按钮的事件。
camickr answered 2019-08-04T17:22:03Z
21 votes
JTextField function=new JTextField(8);
function.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//statements!!!
}});
你需要做的就是将addActionListener添加到JTextField中,如上所述! 按Enter后,操作将在语句中执行您想要的操作!
kyorilys answered 2019-08-04T17:22:29Z
12 votes
为KeyPressed添加活动。
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
// Enter was pressed. Your code goes here.
}
}
Ionică Bizău answered 2019-08-04T17:22:54Z
5 votes
你想做这样的事吗?
JTextField mTextField = new JTextField();
mTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
// something like...
//mTextField.getText();
// or...
//mButton.doClick();
}
}
});
Diego Plascencia Lara answered 2019-08-04T17:23:19Z
1 votes
首先在JButton或JTextField上添加动作命令:
JButton.setActionCommand("name of command");
JTextField.setActionCommand("name of command");
然后将ActionListener添加到JTextField和JButton。
JButton.addActionListener(listener);
JTextField.addActionListener(listener);
之后,On you ActionListener实现写
@Override
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Your actionCommand for JButton") || actionCommand.equals("Your actionCommand for press Enter"))
{
//Do something
}
}
greg answered 2019-08-04T17:23:58Z
1 votes
其他答案(包括已接受的答案)都很好,但如果您已经使用Java 8,则可以执行以下操作(以更短,更新的方式):
textField.addActionListener(
ae -> {
//dostuff
}
);
正如所接受的答案所说,你可以简单地对ActionListener做出反应,它会捕获Enter-Key。
但是,我的方法受益于Java 8中引入的功能概念。
如果要对按钮和JTextField使用相同的操作,则可以执行以下操作:
ActionListener l = ae -> {
//do stuff
}
button.addActionListener(l);
textField.addActionListener(l);
如果需要进一步说明,请告诉我!
Thomas Böhm answered 2019-08-04T17:24:52Z
0 votes
如果要在JTextField输入中设置默认按钮操作,则必须执行以下操作:
//put this after initComponents();
textField.addActionListener(button.getActionListeners()[0]);
它是[0],因为按钮可以有很多动作,但通常只有一个(ActionPerformed)。
JACA answered 2019-08-04T17:25:24Z
-2 votes
public void keyReleased(KeyEvent e)
{
int key=e.getKeyCode();
if(e.getSource()==textField)
{
if(key==KeyEvent.VK_ENTER)
{
Toolkit.getDefaultToolkit().beep();
textField_1.requestFocusInWindow();
}
}
要在JTextField中为“Enter press”写入逻辑,最好将逻辑保留在keyReleased()块而不是keyTyped()&keyPressed()。
Avnish alok answered 2019-08-04T17:25:52Z
-3 votes
只需使用此代码:
SwingUtilities.getRootPane(myButton).setDefaultButton(myButton);
iamprogrammer answered 2019-08-04T17:26:18Z