gpt4 book ai didi

java - 在 Java 文本字段中使用鼠标选择粘贴/替换字符(具有指定的字符限制)

转载 作者:行者123 更新时间:2023-12-01 08:48:52 25 4
gpt4 key购买 nike

我扩展了 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.");
}
}
}

enter image description here

当我启动上述程序并尝试使用 Ctrl+V 将一些新字符粘贴到现有字符之上时,它会将我带到 DocumentCharLimitFilter 的 Replace() 方法的 else 循环,我得到:

Character limit of 1 exceeded.

根据我对这一切的理解,我使用鼠标和 Ctrl+V 粘贴的新字符被视为现有字符之上的附加字符,并且我收到上述消息。当我使用键盘输入字符时,它工作得很好。如果我将 MAX_CHAR_LIMIT 增加到 2,我可以粘贴文本,但现在当我从键盘输入时,我可以输入两个我不想要的字符。

如何使上述代码的工作方式类似于正常粘贴到现有文本之上并替换它,使用 Ctr + V 并将键盘字符限制为指定的限制?我是 Java 初学者。如果我需要提供任何信息,我会很乐意这样做。谢谢。

更新:@camickr、@VGR 非常感谢!我没有注意 replace() 方法中的 length 参数。这是我在 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.");
}
}

最佳答案

您忽略了传递给替换方法的length参数,该参数包含要替换的现有字符数。

如果您在 JTextField 中突出显示一个或多个字符,然后键入或粘贴一些新文本,length 将包含一个正值。 JTextField 内容的新长度将为 oldTextLength - length + newText.length()(除非您更改它)。

关于java - 在 Java 文本字段中使用鼠标选择粘贴/替换字符(具有指定的字符限制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42512743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com