专注于Keylistener

时间:2013-11-29 21:19:10

标签: java swing jbutton keylistener setfocus

所以我得到的是我想尝试制作一个小小的登录小程序。我正在使用Java Windowsbuilder,让它变得更容易。我希望代码不要乱,因为我只是一个初学者。

我遇到的问题是,我的JButton“登录”只会在选择trought选项卡或第一次单击它时注册一个keyevent。我想要的是我只需按“ENTER”键即可使用按钮。

希望你们解决我的问题:)

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.event.KeyAdapter;

public class nummer1 extends JFrame{

private JPanel contentPane;
public static nummer1 theFrame;
private JTextField textFieldUsername;
private JTextField textFieldPass;
private nummer1  me;
private JLabel lblCheck;
private String password = "test", username = "test";

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                nummer1 frame = new nummer1();
                nummer1.theFrame = frame;
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public void Check(){
    String Pass = textFieldPass.getText();
    String Username = textFieldUsername.getText();

    System.out.println(Pass);

    if (Pass.equals(password) && Username.equals(username)){

        lblCheck.setText("Correct Login");

    }else{

        lblCheck.setText("Invalid username or password");

    }
}



/**
 * Create the frame.
 */
public nummer1() {
    me = this;

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 356, 129);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(10, 11, 61, 14);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setBounds(10, 36, 61, 14);
    contentPane.add(lblPassword);

    textFieldUsername = new JTextField();
    textFieldUsername.setBounds(81, 8, 107, 20);
    contentPane.add(textFieldUsername);
    textFieldUsername.setColumns(10);
    me.textFieldUsername = textFieldUsername;

    textFieldPass = new JTextField();
    textFieldPass.setBounds(81, 33, 107, 20);
    contentPane.add(textFieldPass);
    textFieldPass.setColumns(10);
    me.textFieldPass = textFieldPass;

    JButton btnLogin = new JButton("Login");
    contentPane.requestFocusInWindow();
    btnLogin.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                me.Check();
                System.out.println("hi");
            }
        }
    });

    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            me.Check();
        }
    });
    btnLogin.setBounds(198, 7, 89, 23);
    contentPane.add(btnLogin);

    JLabel lblCheck = new JLabel("");
    lblCheck.setHorizontalAlignment(SwingConstants.TRAILING);
    lblCheck.setBounds(10, 65, 264, 14);
    contentPane.add(lblCheck);
    me.lblCheck = lblCheck;
    contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new         
            Component[]{lblUsername, lblPassword, textFieldUsername, textFieldPass,   btnLogin, lblCheck}));

}
}

谢谢Emil!

2 个答案:

答案 0 :(得分:2)

  

我想要的是我只需按下" ENTER"就可以使用按钮。键

听起来你想要#34;登录"按下对话框的默认按钮。

有关问题和解决方案,请参阅Enter Key and Button

答案 1 :(得分:0)

使用面板时,除非面板可对焦,否则KeyListeners将无法工作。你可以做的是使用面板的InputMap和ActionMap对ENTER键进行KeyBinding。

public class Sample extends JPanel{
    //Code
    Sample() {
          //More code
          this.getInputMap().put(KeyStroke.getKeyStroke((char)KeyEvent.VK_ENTER), "Enter" );
          this.getActionMap().put("Enter", new EnterAction());
    }
    private class EnterAction extends AbstractAction(){
        @Override
        public void ActionPerformed(ActionEvent e){
            //Acion
        }
    }
}