栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

停止在Java中输入JTextField后如何启动函数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

停止在Java中输入JTextField后如何启动函数

使用Swing

Timer
和a
documentListener
每次
document
更新时,重置
Timer

看看如何使用Swing计时器和侦听文档中的更改以了解更多详细信息

作为一个简单的例子…

import java.awt.EventQueue;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.event.documentEvent;import javax.swing.event.documentListener;public class Test {    public static void main(String[] args) {        new Test();    }    public Test() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {         ex.printStackTrace();     }     Jframe frame = new Jframe("Testing");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.add(new TestPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    public class TestPane extends JPanel {        public TestPane() { JTextField field = new JTextField(20); JLabel label = new JLabel("Waiting"); DeferreddocumentListener listener = new DeferreddocumentListener(1000, new ActionListener() {     @Override     public void actionPerformed(ActionEvent e) {         // Execute your required functionality here...         label.setText(label.getText() + ".");     } }, true); field.getdocument().adddocumentListener(listener); field.addFocusListener(new FocusListener() {     @Override     public void focusGained(FocusEvent e) {         listener.start();     }     @Override     public void focusLost(FocusEvent e) {         listener.stop();     } }); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER; add(field, gbc); add(label, gbc);        }    }    public class DeferreddocumentListener implements documentListener {        private final Timer timer;        public DeferreddocumentListener(int timeOut, ActionListener listener, boolean repeats) { timer = new Timer(timeOut, listener); timer.setRepeats(repeats);        }        public void start() { timer.start();        }        public void stop() { timer.stop();        }        @Override        public void insertUpdate(documentEvent e) { timer.restart();        }        @Override        public void removeUpdate(documentEvent e) { timer.restart();        }        @Override        public void changedUpdate(documentEvent e) { timer.restart();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号