我必须在 JavaFX 中的 TextBox、TextFile 等组件中显示字符串。此字符串可以包含控制字符、不可打印字符(即所有字符在 ASCII < 32 中)。当我通过 println() 在控制台上显示我的字符串时,控制字符将被空格替换(仅在视图中),因为我可以复制并粘贴相同的字符串仍然带有控制字符。默认情况下,不可打印字符被跳过以进行显示。我想收到相同的结果以在任何 JAvaFx 组件中显示/输入特殊字符,就像它在 Java 控制台中一样,有可能吗?
问问题
1383 次
1 回答
0
随意使用和/或编辑我制作的这段代码:
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
将其设置为任何可显示的。我可能会使用问号。char
setReplacementChar
于 2013-04-06T04:12:32.767 回答