gpt4 book ai didi

java - 按下 JButton 并按下 Enter 键

转载 作者:行者123 更新时间:2023-12-01 12:42:04 26 4
gpt4 key购买 nike

我正在做这个程序,这个框架注册了新用户。

我想要的是能够用信息填写每个文本字段,然后按“Cadastrar”键(“Cadastrar”=“注册”),不仅可以使用鼠标,还可以使用“Enter” “键。

我尝试使用 keyListener 但结果让我有点困惑。代码如下:

package grafico;


public class TelaDeCadastro extends JFrame {

private TextField campoConfirmaSenha;
private TextField campoNome;
private TextField campoEmail;
private TextField campoSenha;
private TextField dicaDeSenha;

public static void main(String[] args) {

public TelaDeCadastro() {
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage(
TelaDeCadastro.class.getResource("/Files/CashLog.png")));
setTitle("Cadastro");
setPreferredSize(new Dimension(400, 300));
setLocationRelativeTo(null);

JButton botaoCadastrar = new JButton("Cadastrar");
botaoCadastrar.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {

}
}
});
botaoCadastrar.setBounds(139, 196, 115, 35);
JButton botaoVoltar = new JButton("Voltar");
botaoVoltar.setBounds(10, 231, 90, 30);
JButton botaoSair = new JButton("Sair");
botaoSair.setBounds(294, 231, 90, 30);

ButtonGroup botoesRetorno = new ButtonGroup();
botoesRetorno.add(botaoSair);
botoesRetorno.add(botaoVoltar);

// botão para submeter as informações passadas
botaoCadastrar.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
});

// botão sair fecha o programa
botaoSair.addActionListener(new ActionListener() {

// botão voltar retorna para a tela de login
botaoVoltar.addActionListener(new ActionListener() {

JPanel container = new JPanel();
container.setLayout(null);

dicaDeSenha = new TextField();
dicaDeSenha.setBounds(109, 159, 265, 22);
container.add(dicaDeSenha);

campoConfirmaSenha = new TextField();
campoConfirmaSenha.setEchoChar('*');
campoConfirmaSenha.setBounds(138, 126, 236, 23);
container.add(campoConfirmaSenha);
campoSenha = new TextField();
campoSenha.setEchoChar('*');
campoSenha.setBounds(109, 93, 265, 23);
container.add(campoSenha);
campoEmail = new TextField();
campoEmail.setBounds(109, 62, 265, 23);
container.add(campoEmail);
campoNome = new TextField();
campoNome.setBounds(109, 31, 265, 23);
container.add(campoNome);

JLabel labelNome = new javax.swing.JLabel("Seu nome:");
labelNome.setBounds(10, 35, 364, 14);
container.add(labelNome);
JLabel labelEmail = new javax.swing.JLabel("Seu Email:");
labelEmail.setBounds(10, 66, 364, 14);
container.add(labelEmail);
JLabel labelSenha = new javax.swing.JLabel("Sua senha:");
labelSenha.setBounds(10, 95, 364, 14);
container.add(labelSenha);

JLabel lblConfirmarSenha = new JLabel("Confirmar senha:");
lblConfirmarSenha.setBounds(10, 126, 122, 15);
container.add(lblConfirmarSenha);

JLabel lblDicaDaSenha = new JLabel("Dica da senha:");
lblDicaDaSenha.setBounds(10, 162, 90, 14);
container.add(lblDicaDaSenha);
container.add(botaoCadastrar);
container.add(botaoVoltar);
container.add(botaoSair);
getContentPane().add(container);

JLabel lblCadastrese = new JLabel("Cadastre-se:");
lblCadastrese.setHorizontalAlignment(SwingConstants.CENTER);
lblCadastrese.setHorizontalTextPosition(SwingConstants.CENTER);
lblCadastrese.setBounds(10, 9, 364, 14);
container.add(lblCadastrese);

JLabel label = new JLabel("");
label.setIcon(new ImageIcon(TelaDeCadastro.class
.getResource("/Files/conta-sem-tarifa.jpg")));
label.setBounds(0, 0, 400, 300);
container.add(label);
pack();
}

public TextField getCampoConfirmaSenha() {

public TextField getCampoNome() {

public TextField getCampoEmail() {

public TextField getCampoSenha() {

public TextField getDicaDeSenha() {
}

最佳答案

不要将 KeyListener(或 MouseListener)与按钮一起使用。

按钮由 ActionListener API 支持,该 API 处理 EnterSpace、其他平台特定的激活按键、鼠标左键单击和助记符自动...

看看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listener了解更多详情。

您还应该看看How to Use Root Panes ,因为 JRootPane 允许您定义一个“默认”按钮,当用户按下“激活”击键时该按钮将被激活。请注意,如果具有焦点的组件消耗该事件,它不会激活按钮

一般来说,您应该尽可能避免使用 KeyListener 并优先使用 key bindings API无论如何,还是

关于java - 按下 JButton 并按下 Enter 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007113/

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