常常有这样的需求,填完一些JTextField后,直接按Enter键就执行动作,而不需要再按一下按钮。本来以为在加一个Keytyped事件的侦听器,然后读取输入的字符就可以确定了,但是试了几次都没有成功。后来查了一下JDK API,看到这样的话:
How the text field consumes VK_ENTER events depends on whether the text field has any action listeners. If so, then VK_ENTER results in the listeners getting an ActionEvent, and the VK_ENTER event is consumed. This is compatible with how AWT text fields handle VK_ENTER events. If the text field has no action listeners, then as of v 1.3 the VK_ENTER event is not consumed. Instead, the bindings of ancestor components are processed, which enables the default button feature of JFC/Swing to work.
看来是要用ActionLinstener, 如没有用的话,就会传给上层组件。
分享到:
相关推荐
当你将一个类用作事件监听器时,你已经设置好一个特定的事件类型,它会用该类进行监听。接下来的操作是:一个匹配的监听器必须被加入到该组件中 组件被创建之后,可以在组件上调用如下方法来将监听器与它联系起来 ...
public class findmin extends Applet implements ActionListener JavaAppliet程序,在文本框里输入三个数,显示最小数
logonButton.addActionListener( //登录按钮事件处理 new ActionListener(){ public void actionPerformed(ActionEvent evt){ char[] pw=password.getPassword(); //得到密码 String message=...
ActionListener
mport java.awt.*; import javax.swing.*;...public class work extends JFrame implements ActionListener { private JTextField text_char,text_uni; private Button button_char,button_clear; public work()
1、 自定义类说明: (1)类名:MainTest 作用:主类,构建主界面 继承的父类:Jframe 主要实现接口:ActionListener 主要成员变量: private JPanel ContentPane; private JButton Insert = new JButton(); private...
1) 创建一个简单的JFrame,使用不同的颜色、线条绘制各种形状,使用不同的字体、颜色绘制字符串,使用最简单的监听器ActionListener对JButton的点击事件编程; 2)使用鼠标事件进行绘图; 3)使用边缘布局管理器和...
action与actionListener的区别
JTextField jurl=new JTextField(60); JEditorPane jEditorPanel=new JEditorPane(); JScrollPane scrollPane=new JScrollPane(jEditorPanel); // JWindow window=new JWindow(WebBrowser.this); // Toolkit ...
public class MyJFrame extends JFrame implements ActionListener { public JTextField name; public JPasswordField password; public File file; public String name_str; public String pw_str; public ...
算法分析 在实现功能时只用了简单的面向对象的设计方法,在处理多个按钮事件时, 区分按钮的关键是按钮的标题区分,我用了按钮命令事件处理,并用 if else 来 区分"添加","删除","修改"这三个按钮。界面上放入一...
public class Calculator extends JFrame implements ActionListener{ JFrame jf; JTextField jtf; JButton jishu = new JButton("="); String sign[] = new String[]{"+","-","*","/","."}; String ops[] = ...
jbGetFile.addActionListener(new ActionListener(){ //按钮事件处理 public void actionPerformed(ActionEvent ent){ try{ jlInfo.setText("正在读取"); URL url=new URL(jtfUrl.getText()); //...
ate final String[] COMMAND={"Backspace","CE","C"}; private final String[] M={" ","MC","MR","MS","M+"}; private JButton keys[]=new JButton[KEYS.length]; private JButton commands[]=new JButton[COMMAND....
private JTextField jTextField2 = null; private JTextField jTextField3 = null; private JTextField jTextField4 = null; private JTextField jTextField5 = null; private JButton jButton = null; ...
jbGetImage.addActionListener(new ActionListener(){ //按钮事件处理 public void actionPerformed(ActionEvent ent){ try{ String urlStr=jtfUrl.getText(); //得到图像的URL地址 URL url=...
这是我用来测试的一个工程,都不能在rich:dataTable中使用action actionListener 博文链接:https://insomniask.iteye.com/blog/111030
logonButton.addActionListener(//登录按钮事件处理 new ActionListener(){ public void actionPerformed(ActionEvent evt){ char[] pw=password.getPassword();//得到密码 String message="您的...