隨意使用和/或編輯該代碼我提出:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
* A text field where all control characters are displayed as the same optional
* char. The default replacement char is space.
*/
public class MyTextField extends JTextField {
private static final long serialVersionUID = -4027593772585498003L;
private static final Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();
//String whose control characters aren't replaced
private String ctrlText;
//Char for replacing control characters
private char replacementChar;
public MyTextField() {
this(null, 0);
}
public MyTextField(int columns) {
this(null, columns);
}
public MyTextField(String text) {
this(text, 0);
}
public MyTextField(String text, int columns) {
ctrlText = (text == null) ? "" : text;
setReplacementChar(' ');
setDocument(new CustomDocument());
setText(text);
setColumns(columns);
}
/**
* Sets the char to replace the control characters.
*
* @param ch the char to replace the control characters
*/
public void setReplacementChar(char ch) {
replacementChar = ch;
}
@Override
public void cut() {
int start = getSelectionStart();
int end = getSelectionEnd();
//Copy to clipboard
String data = ctrlText.substring(start, end);
clipboard.setContents(new StringSelection(data), null);
//Set new text
String newText = ctrlText.substring(0, start) + ctrlText.substring(end);
setText(newText);
}
@Override
public void copy() {
int start = getSelectionStart();
int end = getSelectionEnd();
//Copy to clipboard
String data = ctrlText.substring(start, end);
clipboard.setContents(new StringSelection(data), null);
}
@Override
public void paste() {
int start = getSelectionStart();
int end = getSelectionEnd();
//Get data from clipboard
String data = null;
try { data = (String)clipboard.getData(DataFlavor.stringFlavor); }
catch (UnsupportedFlavorException e) {}
catch (IOException e) {}
//Set new text
String newText = ctrlText.substring(0, start)
+ data
+ ctrlText.substring(end);
setText(newText);
}
/**
* Replaces all control characters with the replacement char.
*
* @param text the String whose control characters are to be replaced
* @return the resulting String
*/
private String replaceCtrlChars(String text) {
if(text == null) return null;
return text.replaceAll("\\p{Cntrl}", "" + replacementChar);
}
/**
* Document where control characters are displayed as the replacement char.
*/
private class CustomDocument extends PlainDocument {
@Override
public void remove(int offs, int len) throws BadLocationException {
//Remove from ctrlText
ctrlText = ctrlText.substring(0, offs)
+ ctrlText.substring(offs + len);
//Remove from text field
super.remove(offs, len);
}
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
//Insert to ctrlText
ctrlText = ctrlText.substring(0, offs)
+ str
+ ctrlText.substring(offs);
//Insert String with replaced chars to text field
super.insertString(offs, replaceCtrlChars(str), a);
}
private static final long serialVersionUID = -6187351778620427126L;
}
}
這其中,控制字符(那些ASCII < 32或== 127),顯示爲同一文本字段,可選char
。
缺省更換char
是空間,但你可以通過調用setReplacementChar
將其設置爲任何可顯示char
。我可能會用一個問號。