首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JFrame,如何让enter键触发与按下按钮相同的结果?

使用JFrame,如何让enter键触发与按下按钮相同的结果?
EN

Stack Overflow用户
提问于 2013-06-05 04:09:48
回答 2查看 1.6K关注 0票数 1

我已经找过了,但我还没有找到我能理解的答案。目前,我已经找到了一些关于触发所有事件之类的问题的帖子,但我只需要触发一个事件。此代码用于登录框。

编辑:我需要回车触发的按钮是登录按钮。

下面是我的代码:

代码语言:javascript
运行
复制
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.io.Serializable;
/**
 * Write a description of class Login here.
 * 
 * @author () 
 * @version ()
*/
public class LoginFrame extends JFrame
{
private JTextField statusField = new JTextField(20);
private JTextField usernameField = new JTextField(10);
private String usernameText;
private JTextField passwordField = new JTextField(10);
private String passwordText;
private JButton loginButton = new JButton("Log in");
private JButton cancelButton = new JButton("Cancel");

public static void main() {
    LoginFrame app = new LoginFrame();
    app.setVisible(true);
    app.setLocationRelativeTo(null);
}

public LoginFrame() {
    super("Login");
    statusField.setText("Enter Username and Password");
    statusField.setHorizontalAlignment(JLabel.CENTER);
    statusField.setEditable(false);
    add(statusField, BorderLayout.NORTH);
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(2, 2));
    p.add(new JLabel("User name:"));
    p.add(usernameField);
    usernameText = usernameField.getText();
    p.add(new JLabel("Password:"));
    p.add(passwordField);
    passwordText =passwordField.getText();
    add(p, BorderLayout.CENTER);
    Box buttonBar = Box.createHorizontalBox();
    buttonBar.add(Box.createHorizontalGlue());
    buttonBar.add(cancelButton);
    buttonBar.add(Box.createHorizontalGlue());
    buttonBar.add(loginButton);
    buttonBar.add(Box.createHorizontalGlue());
    add(buttonBar, BorderLayout.SOUTH);
    cancelButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent cancel) {
                System.exit(0);
            }
        });
    loginButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent login) {
                statusField.setText("Authenticating...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    //Handle exception
                }
                if ((usernameText == "abc") && (passwordText == "123"))
                {
                    statusField.setText("Valid Username and Password");
                }
                else
                {
                    statusField.setText("Invalid: Locked Out");
                    usernameField.setEditable(false);
                    passwordField.setEditable(false);
                }

            }
        });
    p.setPreferredSize(new Dimension(335, 55));
    pack();
}

}

EN

回答 2

Stack Overflow用户

发布于 2013-06-05 07:01:46

是否当您在用户名/密码字段上按回车键时,事件将被触发?只需在这些字段中添加ActionListener,当您按Enter键时,它将自动触发事件。

票数 1
EN

Stack Overflow用户

发布于 2015-05-10 08:26:40

代码语言:javascript
运行
复制
//Right click on your jtextfield Proprieties/Events/KeyPressed

//Inside the event
int key = evt.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
//Your Code

}//No Else
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16926592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档