【问题标题】:Default button in JFrame is not firing when the enter key is being pressed按下回车键时,JFrame 中的默认按钮不会触发
【发布时间】:2012-03-08 02:13:51
【问题描述】:

我有一个JFrame,上面有三个 JButton。我已将txtSearchJTextField 组件)设置为在JFrame 加载时获得焦点。其中一个按钮被设置为默认按钮。这是我的代码:

private void formWindowOpened(java.awt.event.WindowEvent evt) 
{
     // btnRefresh.setMnemonic(KeyEvent.VK_R); // Even if this line 
                                               // is not commented, but
                                               // still the event wouldn't fire.
     this.getRootPane().setDefaultButton(btnRefresh);
}

当它加载时,按钮只是被选中,但是当按下 Enter 键时它什么也没做。如何正确实现?

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

private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                           
    JOptionPane.showMessageDialog(this, "Pressed!");
    // Other codes here (Replace by JOptionPane)
}  

【问题讨论】:

  • 我发现最简单的方法是在 JFrame 可见后调用 requestFocusInWindow() 来设置默认按钮。希望这对某人有所帮助。

标签: java swing netbeans default


【解决方案1】:

JFrame 出现时哪个组件具有焦点?我问是因为某些组件“吃掉”了 Enter 键事件。例如,JEditorPane 会这样做。

此外,当您将ActionListener 分配给JTextField 时,将调用ActionListener 而不是根窗格的DefaultButton。您必须选择使用ActionListenerDefaultButton,但您不能同时使用JTextField。我相信这也适用于其他组件。

【讨论】:

  • 我已将JTextField 设置为在JFrame 加载时聚焦。
  • 正确分析,不是解决方案 :-) @johntotetwoo 从您接受这一点我认为它指向了解决方案的方向 - 您可以考虑使用该解决方案编辑您的问题
  • Brilliant.. 我的密码字段上有一个 actionListener,但未能弄清楚这是默认按钮没有响应的原因。谢谢
【解决方案2】:

从发布的内容中,我看不出你做错了什么。这是一个有效的简短示例。也许它会揭示一些对你有用的东西。

import java.awt.BorderLayout; 
public class ExampleFrame extends JFrame 
{
  private JPanel    m_contentPane;
  private JTextField m_textField;

  /**
   * Launch the application.
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                ExampleFrame frame = new ExampleFrame();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
  }

  /**
  * Create the frame.
   */
  public ExampleFrame()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    m_contentPane = new JPanel();
    m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    m_contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(m_contentPane);

    m_textField = new JTextField();
    m_contentPane.add(m_textField, BorderLayout.NORTH);
    m_textField.setColumns(10);

    JButton btnNewButton = new JButton("Default");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(ExampleFrame.this, "Default.");
        }
    });
    m_contentPane.add(btnNewButton, BorderLayout.CENTER);

    JButton btnNewButton_1 = new JButton("Not default");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showMessageDialog(ExampleFrame.this, "Not default.");
        }
    });
    m_contentPane.add(btnNewButton_1, BorderLayout.WEST);
    m_textField.requestFocus();
    getRootPane().setDefaultButton(btnNewButton);
  }
}

【讨论】:

  • 向 textField 添加一个 actionListener 看看会发生什么 :-)
  • 你成功了,克娄巴特拉!当我放入 ActionListener 时,默认按钮无法触发。 johntotetwoo,您需要删除动作侦听器以触发默认按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多