限制JTextField中的字符数

我想设置最大长度JTextField,这样您输入的字符数不能超过限制。这是我到目前为止的代码...


    textField = new JTextField();

    textField.setBounds(40, 39, 105, 20);

    contentPane.add(textField);

    textField.setColumns(10);

有什么简单的方法可以限制字符数?


肥皂起泡泡
浏览 536回答 3
3回答

繁华开满天机

您可以执行以下操作(从此处获取):import java.awt.FlowLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.PlainDocument;class JTextFieldLimit extends PlainDocument {&nbsp; private int limit;&nbsp; JTextFieldLimit(int limit) {&nbsp; &nbsp; super();&nbsp; &nbsp; this.limit = limit;&nbsp; }&nbsp; JTextFieldLimit(int limit, boolean upper) {&nbsp; &nbsp; super();&nbsp; &nbsp; this.limit = limit;&nbsp; }&nbsp; public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {&nbsp; &nbsp; if (str == null)&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if ((getLength() + str.length()) <= limit) {&nbsp; &nbsp; &nbsp; super.insertString(offset, str, attr);&nbsp; &nbsp; }&nbsp; }}public class Main extends JFrame {&nbsp; JTextField textfield1;&nbsp; JLabel label1;&nbsp; public void init() {&nbsp; &nbsp; setLayout(new FlowLayout());&nbsp; &nbsp; label1 = new JLabel("max 10 chars");&nbsp; &nbsp; textfield1 = new JTextField(15);&nbsp; &nbsp; add(label1);&nbsp; &nbsp; add(textfield1);&nbsp; &nbsp; textfield1.setDocument(new JTextFieldLimit(10));&nbsp; &nbsp; setSize(300,300);&nbsp; &nbsp; setVisible(true);&nbsp; }}编辑:看看这个以前的SO职位。您可以拦截按键事件,并根据文本字段中当前的字符数添加/忽略它们。

慕的地6264312

自从DocumentFilterJava 1.4中引入以来,Document就已经覆盖了重写的 需求。DocumentFilter提供了一种Document在实际到达之前过滤传递给的内容的方法。这些使字段可以继续维护它需要的文档,同时提供过滤来自用户的输入的手段。import java.awt.EventQueue;import java.awt.GridBagLayout;import javax.swing.JFrame;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.text.AbstractDocument;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.DocumentFilter;public class LimitTextField {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new LimitTextField();&nbsp; &nbsp; }&nbsp; &nbsp; public LimitTextField() {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JTextField pfPassword = new JTextField(20);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((AbstractDocument)pfPassword.getDocument()).setDocumentFilter(new LimitDocumentFilter(15));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Testing");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLayout(new GridBagLayout());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(pfPassword);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public class LimitDocumentFilter extends DocumentFilter {&nbsp; &nbsp; &nbsp; &nbsp; private int limit;&nbsp; &nbsp; &nbsp; &nbsp; public LimitDocumentFilter(int limit) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (limit <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Limit can not be <= 0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.limit = limit;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int currentLength = fb.getDocument().getLength();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int overLimit = (currentLength + text.length()) - limit - length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (overLimit > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = text.substring(0, text.length() - overLimit);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (text.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.replace(fb, offset, length, text, attrs);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java