我扩展了DocumentFilter类,以便在文本字段中将要输入的字符数限制为指定的数字。这是我的SSCE:
主要课程:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.text.PlainDocument;
public class Sandbox implements Runnable {
private JFrame frame;
private JTextField inputField;
private JButton searchButton;
private int MAX_CHAR_LIMIT = 1;
public Sandbox() {
inputField = new JTextField();
inputField.setColumns(10);
inputField.setFont(new Font(null, Font.BOLD, 20));
}
@Override
public void run() {
frame = new JFrame("SSCE");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 300));
PlainDocument doc = (PlainDocument) inputField.getDocument();
doc.setDocumentFilter(new DocumentCharLimitFilter(MAX_CHAR_LIMIT));
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(inputField);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Sandbox());
}
}
DocumentCharLimitFilter类
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class DocumentCharLimitFilter extends DocumentFilter {
private int MAX_CHAR_LIMIT;
public DocumentCharLimitFilter(int maxChars) {
this.MAX_CHAR_LIMIT = maxChars;
}
// You don't need insertString() when entering text in JTextField.
@Override
public void replace(FilterBypass fb, int offset, int length, String newText, AttributeSet aSet) throws BadLocationException {
int oldTextLength = fb.getDocument().getLength();
String oldText = fb.getDocument().getText(0, oldTextLength);
String combinedText = oldText + newText;
if (combinedText.length() <= MAX_CHAR_LIMIT) {
super.replace(fb, offset, length, newText, aSet);
// paste characters upto maximum allowed limit
} else if (oldTextLength < MAX_CHAR_LIMIT) {
int cutPosition = MAX_CHAR_LIMIT - oldTextLength;
String cutToFit = newText.substring(0, cutPosition);
super.replace(fb, offset, length, cutToFit, aSet);
} else {
System.out.println("Character limit of " + MAX_CHAR_LIMIT + " exceeded.");
}
}
}
当我启动上面的程序并尝试使用Ctrl + V在现有字符的顶部粘贴一些新字符时,我需要使用DocumentCharLimitFilter的replace()方法的其他循环来获取:
超出了字符数限制。
根据我的理解,我使用鼠标和Ctrl + V粘贴的新角色被视为现有角色之上的附加角色,我得到了上述消息。当我使用键盘输入字符时,它工作得很好。如果我将MAX_CHAR_LIMIT增加到2,我可以粘贴文本,但现在当我从键盘输入时,我可以输入两个我不想要的字符。
如何使上面的代码在现有文本的基础上正常粘贴并使用Ctr + V替换它,并将键盘字符限制为指定的限制?我是Java的初学者。如果我需要提供任何信息,我很乐意这样做。谢谢。
更新:@camickr,@ VGR非常感谢你!
我没有注意length
方法中的replace()
参数。这就是我在DocumentCharLimitFilter类中所做的:
@Override
public void replace(FilterBypass fb, int offset, int length, String newText, AttributeSet aSet) throws BadLocationException {
System.out.println(fb.getClass());
int oldTextLength = fb.getDocument().getLength();
String oldText = fb.getDocument().getText(0, oldTextLength);
String combinedText = oldText + newText;
if (combinedText.length() <= MAX_CHAR_LIMIT) {
super.replace(fb, offset, length, newText, aSet);
// paste characters upto maximum allowed limit
} else if (oldTextLength < MAX_CHAR_LIMIT) {
int cutPosition = MAX_CHAR_LIMIT - oldTextLength;
String cutToFit = newText.substring(0, cutPosition);
super.replace(fb, offset, length, cutToFit, aSet);
// NEW CODE
// http://stackoverflow.com/questions/42512743/pasting-replacing-a-character-using-mouse-selection-in-java-textfield-with-spec
// length indicates number of characters highlighted using mouse or keyboard. This will work only when
// the entire text is highlighted (if entire text is not highlighted, it would get complicated)
// and when it is highlighted, I make sure that the new text to be pasted is within limits of the MAX_CHAR_LIMIT
} else if (length == MAX_CHAR_LIMIT) {
String correctedString = newText;
if (newText.length() > MAX_CHAR_LIMIT) {
correctedString = newText.substring(0, MAX_CHAR_LIMIT);
}
super.replace(fb, offset, length, correctedString, aSet);
} else {
System.out.println("Entered characters exceed specified limit of " + MAX_CHAR_LIMIT + "exceeded.");
}
}
答案 0 :(得分:1)
您忽略了传递给replace方法的length
参数,该方法包含要替换的现有字符数。
如果您突出显示JTextField中的一个或多个字符,然后键入或粘贴一些新文本,length
将包含正值。 JTextField内容的新长度为oldTextLength - length + newText.length()
(除非您更改)。