【问题标题】:Pressing JButton with the Enter key [duplicate]使用 Enter 键按下 JButton [重复]
【发布时间】:2014-09-20 08:11:24
【问题描述】:

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

我想要的是能够用信息填写每个文本字段,然后按下“地籍”键(“地籍”=“注册”),不仅用鼠标而且用“输入” "键。

我尝试使用 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() {
}

【问题讨论】:

  • 避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
  • 另外,注意混合 AWT(重量级)和 Swing(重量级)组件,这可能不会很好地结束......

标签: java swing jframe jbutton keylistener


【解决方案1】:

不要将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

【讨论】:

  • 是的,你是对的,感谢分享知识。
  • “是的,你是对的” - 并非总是如此,但这是让我恼火的时刻之一 ;)
  • 为什么会激怒你? :)
  • 因为有一个很好的文档和演示的方式来使用 API,它提供了跨多个平台的一致使用。尝试做任何其他事情都会破坏这一点,这只会让我感到恼火:P - 我在多个平台上工作,当一个程序没有充分的理由不符合已知约定时,这让我很恼火:P
  • 有些程序员从不想阅读文档,他们只是把它放在一边,开始像我一样编码:)
猜你喜欢
  • 2016-11-13
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多