我想在輸入JFormattedTextField
中的數字時看到格式化數字。例如,當用戶鍵入124451000時,它必須在屏幕上動態顯示爲124.451.000,而不是在用戶按Tab或Enter鍵時顯示。如何強制JFormattedTextField值動態更改
我希望在輸入第四個數字後緊跟第三個數字(例如2.566)。
public class MyDocListener implements DocumentListener{
NumberFormat nf = NumberFormat.getIntegerInstance(Locale.GERMAN);
NumberFormatter nff = new NumberFormatter(nf);
DefaultFormatterFactory factory = new DefaultFormatterFactory(nff);
final String newline = "\n";
public void insertUpdate(DocumentEvent e) {
updateLog(e, "inserted into");
NumberFormat numberFormatter = new DecimalFormat("#,###,###");
// I put this code to change the format.
}
public void removeUpdate(DocumentEvent e) {
updateLog(e, "removed from");
}
public void changedUpdate(DocumentEvent e) {
//Plain text components don't fire these events.
}
public void updateLog(DocumentEvent e, String action) {
Document doc = (Document)e.getDocument();
int changeLength = e.getLength();
displayArea.append(
changeLength + " character" +
((changeLength == 1) ? " " : "s ") +
action + doc.getProperty("name") + "." + newline +
" Text length = " + doc.getLength() + newline);
}
}
嗯,我在另一個類中嘗試這種代碼,我不能跑,我的JFormattedTextField,我不能在另一個class.I succced無法訪問對象,它是另一個類。
嗯,我做到了,但我不能改變其寫作時間 – Alify
的NumberFormat的NumberFormatter在JFormattedTextField中文字; public class MyDocumentListener implements DocumentListener public void insertUpdate(DocumentEvent e){ numberFormatter = new DecimalFormat(「#,###,###」); } } myJFormattedTextFrield.getDocument()。addDocumentListener(new MyDocumentListener()); – Alify
謝謝EyeOfTheHawks – Alify