当进入键时,执行JButton在面板- Swing中。

时间:2022-09-20 14:42:22

I have Login JButton on a panel and I need to execute it when I press ENTER key.

我在一个面板上有登录JButton,当我按ENTER键时,我需要执行它。

Do we have any code snippet for that?

我们有代码片段吗?

2 个解决方案

#1


2  

You can use InputMap and ActionMap to do this.

您可以使用InputMap和ActionMap来完成这个任务。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ac1
{
    public static void main(String args[])
    {
    JFrame f=new JFrame();
    f.setVisible(true);
    f.setSize(400,400);
    f.setLayout(new FlowLayout());

    final JButton b=new JButton("button");
    f.add(b);



f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDO

W).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"clickButton");

f.getRootPane().getActionMap().put("clickButton",new AbstractAction(){
        public void actionPerformed(ActionEvent ae)
        {
    b.doClick();
    System.out.println("button clicked");
        }
    });
    }
}

#2


1  

To have initial focus on your button you can do something like this:

首先关注你的按钮,你可以这样做:

frame.getRootPane().setDefaultButton(buttonName);
buttonName.requestFocus();

//Or you can bind your Enter key to JComponent and JButton as:

//或者您可以将输入键绑定到JComponent和JButton,如下所示:

AbstractAction action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
        JButton button = (JButton) e.getSource();
        button.doClick();        
        } else if(e.getSource() instanceof JComponent){
            JComponent component = (JComponent) e.getSource();
            component.transferFocus();
        }
    }
    };

//You can bind key to JComponent like:

//您可以将密钥绑定到JComponent:

jComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
jComponent.getActionMap().put("TransferFocus", action);

//You can bind key to JButton like:

//您可以将键绑定到JButton:

jButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "DoClick");
jButton.getActionMap().put("DoClick", action);

Useful Link

有用的链接

How to Use the Focus Subsystem

如何使用焦点子系统?

#1


2  

You can use InputMap and ActionMap to do this.

您可以使用InputMap和ActionMap来完成这个任务。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ac1
{
    public static void main(String args[])
    {
    JFrame f=new JFrame();
    f.setVisible(true);
    f.setSize(400,400);
    f.setLayout(new FlowLayout());

    final JButton b=new JButton("button");
    f.add(b);



f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDO

W).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"clickButton");

f.getRootPane().getActionMap().put("clickButton",new AbstractAction(){
        public void actionPerformed(ActionEvent ae)
        {
    b.doClick();
    System.out.println("button clicked");
        }
    });
    }
}

#2


1  

To have initial focus on your button you can do something like this:

首先关注你的按钮,你可以这样做:

frame.getRootPane().setDefaultButton(buttonName);
buttonName.requestFocus();

//Or you can bind your Enter key to JComponent and JButton as:

//或者您可以将输入键绑定到JComponent和JButton,如下所示:

AbstractAction action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() instanceof JButton){
        JButton button = (JButton) e.getSource();
        button.doClick();        
        } else if(e.getSource() instanceof JComponent){
            JComponent component = (JComponent) e.getSource();
            component.transferFocus();
        }
    }
    };

//You can bind key to JComponent like:

//您可以将密钥绑定到JComponent:

jComponent.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
jComponent.getActionMap().put("TransferFocus", action);

//You can bind key to JButton like:

//您可以将键绑定到JButton:

jButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "DoClick");
jButton.getActionMap().put("DoClick", action);

Useful Link

有用的链接

How to Use the Focus Subsystem

如何使用焦点子系统?