gpt4 book ai didi

java - 在任何文本字段上按回车键返回按钮单击

转载 作者:行者123 更新时间:2023-11-29 07:12:15 25 4
gpt4 key购买 nike

我有一个带有登录按钮的简单 jFrame,我正尝试通过按回车键来触发我的 ActionPerformed 事件。我不想先按“tab”键来聚焦按钮,但只要有人按下回车键,它就会触发。我在这里缺少什么?

编辑:如果更容易做到,当焦点位于“PasswordField”时也可以触发事件,因为这是按下登录按钮之前具有焦点的最后一个字段

根据要求。这是 loginGUI 的完整代码:

package unive.facturatie.boundary;

import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import unive.facturatie.control.LoginManager;

/**
*
* @author Forza
*/
public class LoginGUI extends javax.swing.JFrame {

JFrame frame = new JFrame();
JRootPane rootPane = frame.getRootPane();
//JButton inloggenButton = new JButton("Login");
//frame.getRootPane().setDefaultButton(inloggenButton);

/**
* Creates new form LoginGUI
*/
public LoginGUI() {
frame.getRootPane().setDefaultButton(inloggenButton);
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

initComponents();
this.setLocation(320, 160);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

inlogLabel = new javax.swing.JLabel();
gebruikersnaamLabel = new javax.swing.JLabel();
wachtwoordLabel = new javax.swing.JLabel();
gebruikersnaamTextField = new javax.swing.JTextField();
wachtwoordPasswordField = new javax.swing.JPasswordField();
inloggenButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Facturatie inlogscherm");

inlogLabel.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
inlogLabel.setText("Inloggen");

gebruikersnaamLabel.setText("Gebruikersnaam:");

wachtwoordLabel.setText("Wachtwoord:");

wachtwoordPasswordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
wachtwoordPasswordFieldActionPerformed(evt);
}
});

inloggenButton.setText("Inloggen");
inloggenButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
inloggenButtonActionPerformed(evt);
}
});
inloggenButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
inloggenButtonKeyPressed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(inlogLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(inloggenButton)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(gebruikersnaamLabel)
.addComponent(wachtwoordLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(gebruikersnaamTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
.addComponent(wachtwoordPasswordField)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(inlogLabel)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gebruikersnaamLabel)
.addComponent(gebruikersnaamTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(wachtwoordLabel)
.addComponent(wachtwoordPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(inloggenButton)
.addContainerGap(25, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void inloggenButtonActionPerformed(java.awt.event.ActionEvent evt) {
//frame.getRootPane().setDefaultButton(inloggenButton);
boolean isValid = false;
String username = gebruikersnaamTextField.getText();
String password = wachtwoordPasswordField.getText();

LoginManager loginManager = new LoginManager();
isValid = loginManager.Inloggen(username, password);
if (isValid == true)
{
new MainGUI().setVisible(true);
dispose();
}
else
{
JOptionPane.showMessageDialog(frame, "Gebruikersnaam en/of wachtwoord onjuist!", "Insane error", JOptionPane.ERROR_MESSAGE);
}
}

private void inloggenButtonKeyPressed(java.awt.event.KeyEvent evt) {
//frame.getRootPane().setDefaultButton(inloggenButton);
//inloggenButton.registerKeyboardAction(inloggenButton.getActionForKeyStroke(
//KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
//KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
//JButton.WHEN_IN_FOCUSED_WINDOW);

//inloggenButton.registerKeyboardAction(inloggenButton.getActionForKeyStroke(
//KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
//KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
//JButton.WHEN_IN_FOCUSED_WINDOW);

}

private void wachtwoordPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

// Variables declaration - do not modify
private javax.swing.JLabel gebruikersnaamLabel;
private javax.swing.JTextField gebruikersnaamTextField;
private javax.swing.JLabel inlogLabel;
private javax.swing.JButton inloggenButton;
private javax.swing.JLabel wachtwoordLabel;
private javax.swing.JPasswordField wachtwoordPasswordField;
// End of variables declaration
}

最佳答案

这很简单:

JFrame frame = new JFrame();
JButton btn = new JButton("Login");
frame.getRootPane().setDefaultButton(btn);

就是这样。您不需要 KeyListenerregisterKeyBoardActiongetKeyStroke...只需为您的按钮设置一个 ActionEvent 并设置该按钮为默认值。在窗口打开时按 Enter 将激活默认按钮。

关于java - 在任何文本字段上按回车键返回按钮单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com