Pasting/Replacing 在 Java 文本字段中使用鼠标选择的字符(具有指定的字符限制)
Pasting/Replacing a character using mouse selection in Java textfield (with specified character limit)
我扩展了 DocumentFilter class 以将要输入的字符数限制为文本字段中的指定数字。这是我的 SSCE:
主要class:
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 class
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() 方法的 else 循环,我得到:
Character limit of 1 exceeded.
据我了解,我使用鼠标和 Ctrl+V 粘贴的新字符被视为现有字符之上的附加字符,我收到了上述消息。当我使用键盘输入字符时,它工作得很好。如果我将 MAX_CHAR_LIMIT 增加到 2,我可以粘贴文本,但现在当我从键盘输入时,我可以输入两个我不想要的字符。
如何使上面的代码像正常粘贴现有文本一样工作并替换它,使用 Ctr + V 并仍然将键盘字符限制在指定的限制内?我是 Java 的初学者。如果我需要提供任何信息,我会很乐意这样做。谢谢你。
更新:@camickr,@VGR 非常感谢!
我没有注意 replace()
方法中的 length
参数。这就是我在 DocumentCharLimitFilter class:
中所做的
@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
//
// 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.");
}
}
您忽略了传递给 replace 方法的 length
参数,其中包含要替换的现有字符数。
如果突出显示 JTextField 中的一个或多个字符,然后键入或粘贴一些新文本,length
将包含一个正值。 JTextField 内容的新长度将为 oldTextLength - length + newText.length()
(除非您更改它)。
我扩展了 DocumentFilter class 以将要输入的字符数限制为文本字段中的指定数字。这是我的 SSCE:
主要class:
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 class
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() 方法的 else 循环,我得到:
Character limit of 1 exceeded.
据我了解,我使用鼠标和 Ctrl+V 粘贴的新字符被视为现有字符之上的附加字符,我收到了上述消息。当我使用键盘输入字符时,它工作得很好。如果我将 MAX_CHAR_LIMIT 增加到 2,我可以粘贴文本,但现在当我从键盘输入时,我可以输入两个我不想要的字符。
如何使上面的代码像正常粘贴现有文本一样工作并替换它,使用 Ctr + V 并仍然将键盘字符限制在指定的限制内?我是 Java 的初学者。如果我需要提供任何信息,我会很乐意这样做。谢谢你。
更新:@camickr,@VGR 非常感谢!
我没有注意 replace()
方法中的 length
参数。这就是我在 DocumentCharLimitFilter class:
@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
//
// 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.");
}
}
您忽略了传递给 replace 方法的 length
参数,其中包含要替换的现有字符数。
如果突出显示 JTextField 中的一个或多个字符,然后键入或粘贴一些新文本,length
将包含一个正值。 JTextField 内容的新长度将为 oldTextLength - length + newText.length()
(除非您更改它)。