JTextField实现实时动态监听

第一:想要实时动态的监听文本框每输入的内容,需要利用JTextField的抽象类JTextComponent,调用getDocument()方法来返回一个Document对象 
Java代码   收藏代码
  1. //获取与编辑器关联的模型  
  2. Document doc = textField.getDocument();  


第二:利用获取的Document对象,来添加一个DocumentListener监听器。因为该监听器就可以实现动态的跟踪文本框的输入内容。实现DocumentListener接口,必须重写该接口中的三个方法 

1、void insertUpdate(DocumentEvent e) 
2、void removeUpdate(DocumentEvent e) 
3、void changedUpdate(DocumentEvent e)
 

第三:下面给出一个简短的代码片段,供给大家参考 
Java代码   收藏代码
  1. package com.gxa.edu;  
  2.   
  3. import javax.swing.JFrame;  
  4. import javax.swing.JTextField;  
  5. import javax.swing.text.Document;  
  6. import javax.swing.event.DocumentListener;  
  7. import javax.swing.event.DocumentEvent;  
  8.   
  9. /** 
  10. * author:国信安百杰 
  11. */  
  12. public class TextFieldFrame extends JFrame implements DocumentListener {  
  13.   
  14.     private JTextField textField;  
  15.       
  16.     public TextFieldFrame() {  
  17.         super("动态实时监听TextField");  
  18.         init();  
  19.     }  
  20.       
  21.     public void init() {  
  22.         textField = new JTextField();  
  23.           
  24.         //获取与编辑器关联的模型  
  25.         Document doc = textField.getDocument();  
  26.           
  27.         //添加DocumentListener监听器  
  28.         doc.addDocumentListener(this);  
  29.     }  
  30.       
  31.     /** 
  32.     * 实现DocumentListener接口中insertUpdate方法 
  33.     * 该方法可以跟踪文本框中输入的内容 
  34.     */  
  35.     public void insertUpdate(DocumentEvent e) {  
  36.         Document doc = e.getDocument();  
  37.         String s = doc.getText(0, doc.getLength); //返回文本框输入的内容  
  38.     }  
  39.       
  40.     /** 
  41.     * 实现DocumentListener接口removeUpdate方法 
  42.     * 该方法可以跟踪文本框中移除的内容,例如:在文本框中点击Backspace 
  43.     */  
  44.     public void removeUpdate(DocumentEvent e) {  
  45.         Document doc = e.getDocument();  
  46.         String s = doc.getText(0, doc.getLength); //返回文本框输入的内容  
  47.     }  
  48.       
  49.     /** 
  50.     * 实现DocumentListener接口changedUpdate方法 
  51.     * 该方法可以跟踪当文本框中已存在的内容改变时,获取相应的值 
  52.     */  
  53.     public void changedUpdate(DocumentEvent e) {  
  54.         Document doc = e.getDocument();  
  55.         String s = doc.getText(0, doc.getLength); //返回文本框输入的内容  
  56.     }  
  57.   
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package cn; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; //登陆面板 public class Login extends JFrame { Connection conn = null; Statement stmt = null; ResultSet rst = null; public Login() { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载驱动成功。"); String url = "jdbc:mysql://localhost/users"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); System.out.println("连接数据库成功。"); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rst = stmt.executeQuery("SELECT*FROM user"); } catch (Exception e) { System.out.println(e); } setTitle("登陆页面"); JPanel panel = new JPanel(); //JLabel lblName = new JLabel("用户名:"); JTextField txtName = new JTextField(10); //lblName.setHorizontalAlignment(JLabel.CENTER); panel.add(new JLabel("用户名:")); panel.add(txtName); JLabel lblPassword = new JLabel("密码:"); JPasswordField txtPassword = new JPasswordField(10); lblPassword.setHorizontalAlignment(JLabel.CENTER); panel.add(lblPassword); panel.add(txtPassword); add(panel, BorderLayout.CENTER); JPanel panel2 = new JPanel(); JButton btnLogin = new JButton("登陆"); /** * 给登陆按钮添加监听事件 当按钮被点击时时间被触发 */ btnLogin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean b = false; // boolean b = um.Login(txtName.getText(), txtPassword.getText()); try { stmt = conn.createStatement();// 预定义语句 // 数据库查询语句(根据用户名和密码) String sql = "select * from user where User='" + txtName.getText() + "' and Password='" + txtPassword.getText() + "'"; rst = stmt.executeQuery(sql);// 执行查询语句 // rst中有数据,则将标记改为true if (rst.next()) { b = true; } } catch (SQLException e1) { e1.printStackTrace(); } if (b) {// 登陆成功,跳转页面 JOptionPane.showMessageDialog(null, "登陆成功!"); new ProductQueryDemo();// 打开主页 dispose();// 关闭窗口 } else {// 登陆失败 JOptionPane.showMessageDialog(null, "登陆失败!"); } } }); JButton btnReset = new JButton("重置"); /** * 点击重置按钮时,将文本框中的值设置为空 */ btnReset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { txtName.setText(""); txtPassword.setText(""); } }); JButton btnSignin = new JButton("注册"); /** * 点击注册按钮时,调用注册程序 */ btnSignin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Signin(); dispose();// 关闭窗口 } }); panel2.add(btnLogin); panel2.add(btnReset); panel2.add(btnSignin); add(panel2, BorderLayout.PAGE_END); setSize(430, 150); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Login(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值