如何关联按“输入”点击按钮? [英] How to associate pressing "enter" with clicking button?

查看:193
本文介绍了如何关联按“输入”点击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的swing程序中,我有一个JTextField和一个JButton。我想,一旦用户按下回车键,JButton的actionListener就会运行。我该怎么办?
提前致谢。

In my swing program I have a JTextField and a JButton. I would like for, once the user presses the "enter" key, the actionListener of the JButton runs. How would I do this? Thanks in advance.

推荐答案

JRootPane有一个方法setDefaultButton(JButton按钮)可以做你想要的。如果您的应用程序是JFrame,它会实现RootPaneContainer接口,您可以通过在JFrame上调用getRootPane()来获取根窗格,然后在返回的根窗格上调用setDefaultButton。相同的技术适用于JApplet,JDialog或任何其他实现RootPaneContainer的类。

JRootPane has a method setDefaultButton(JButton button) that will do what you want. If your app is a JFrame, it implements the RootPaneContainer interface, and you can get the root pane by calling getRootPane() on the JFrame, and then call setDefaultButton on the root pane that was returned. The same technique works for JApplet, JDialog or any other class that implements RootPaneContainer.

这篇关于如何关联按“输入”点击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆