0
在NetBeans中,如何爲JFormattedTextField
設置無效字符,以便它不允許:|
,[
,]
等等。如何禁止JFormattedTextField中的字符?
沒關係,我想出瞭解決方案:
MaskFormatter formatter = null;
try {
//# - Any valid number, uses Character.isDigit.
//' - Escape character, used to escape any of the special formatting characters.
//U - Any character (Character.isLetter). All lowercase letters are mapped to upper case.
//L - Any character (Character.isLetter). All upper case letters are mapped to lower case.
//A - Any character or number (Character.isLetter or Character.isDigit)
//? - Any character (Character.isLetter).
//* - Anything.
//H - Any hex character (0-9, a-f or A-F).
formatter = new MaskFormatter("******************************");
formatter.setInvalidCharacters("|[]");
} catch (java.text.ParseException ex) {
}
然後,在GUI上點擊右鍵在JFormattedTextField上,點擊自定義代碼,下拉框旁邊myFormattedTextField = new javax.swing.JFormattedTextField();
選擇自定義創建並添加格式到JFormattedTextField如下:myFormattedTextField = new javax.swing.JFormattedTextField(formatter);
希望這將有助於別人有一天。