TestField设置长度
2015-11-12 13:30
197 查看
import java.awt.BorderLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class TextFieldTest extends javax.swing.JFrame { private JTextField field; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { TextFieldTest inst = new TextFieldTest(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public TextFieldTest() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { field = new LimitedTextField(); getContentPane().add(field, BorderLayout.CENTER); } pack(); this.setSize(400, 70); } catch (Exception e) { e.printStackTrace(); } } class LimitedTextField extends JTextField { private int maxLength = 5; public void setMaxLength(int maxLength) { this.maxLength = maxLength; } public int getMaxLength() { return this.maxLength; } public LimitedTextField() { this.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { int code = e.getKeyChar(); /*if (code == KeyEvent.VK_BACK_SPACE) { e.isActionKey(); } else*/ if (getDocument().getLength() >= maxLength) { e.consume(); } } }); } } }
相关文章推荐
- LeetCode(129) Sum Root to Leaf Numbers
- android某加固脱壳(一)
- LeetCode(129) Sum Root to Leaf Numbers
- 平台顺利通过公安部一所《GBT 28181-2011 安全防范视频监控联网系统信息传输、交换、控制技术要求》检测
- Bozosort: Sorting by Exchanging
- 一句shell命令搞定代码行数统计
- soj 3014: Seek the Name, Seek the Fame (字符串hash)
- Ubuntu下自己编译安装和卸载软件
- Theaon:theaon的图结构
- spring+hibernate工程使用JNDI连接部署至websphere,报错Caused by: java.lang.ClassNotFoundException: oracle.sql.BLO
- Robot FrameworkResource及关键字的使用
- STM32 FOC SDK 上电电机自动旋转
- android Butter Knife 使用详解
- 在Eclipse中修改Jsp页面的新增模板
- 安装分布式文件系统MooseFS
- 整数划分问题
- 房地产O2O应用中为什么很少提供社交入口?
- 结构体内存分配问题
- HBase基本数据操作详解【完整版,绝对精品】
- 图像处理中的数学原理详解11——线性空间