基本上,如果我的用户类型为100000
,TextField将显示100 000
,则我试图动态解析JtextField中的条目
我试图显示一个弹出菜单,但这不是无关紧要的,我试图只是在用户键入键Enter
或当条目失去焦点时进行解析,但这是可行的不太直观。
所以有些代码是可运行的,但我知道解析的方法不是很有效,但实际上行得通...
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.ParseException;
public class Test extends PlainDocument {
private static final long serialVersionUID = 1L;
private final JTextComponent _textField;
public Test(JTextComponent field) {
_textField = field;
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
super.insertString(offs, str, a);
String word = null;
try {
word = autoComplete(getText(0, getLength()));
} catch (ParseException e) {
e.printStackTrace();
}
if (word != null) {
super.insertString(offs + str.length(), word, a);
_textField.setCaretPosition(offs + str.length());
_textField.moveCaretPosition(getLength());
}
}
public String autoComplete(String text) throws ParseException {
NumberFormat nf = NumberFormat.getInstance();
if (nf.parse(text).intValue() > 0) {
return numberParser(text);
}
return null;
}
public static JTextField createAutoCompleteTextField() {
JTextField field = new JTextField(20);
Test doc = new Test(field);
field.setDocument(doc);
return field;
}
public static void main(String args[]) {
//Le jtextField qui sera autocompleté
final JTextField field = Test.createAutoCompleteTextField();
//Action clavier touche entrer
field.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
System.out.println("Text : " + field.getText());
}
}
});
//creation de fenetre principale
JFrame frame = new JFrame("NumberParser");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Entry : "));
frame.add(field);
frame.pack();
frame.setVisible(true);
}
public String numberParser(String data) {
String out = "";
if (data.length() >= 4 && !data.contains(".")) {
switch (data.length()) {
case 4:
out = data.substring(0, 1) + " " + data.substring(1, 4);
break;
case 5:
out = data.substring(0, 2) + " " + data.substring(2, 5);
break;
case 6:
out = data.substring(0, 3) + " " + data.substring(3, 6);
break;
case 7:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7);
break;
case 8:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8);
break;
case 9:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9);
break;
case 10:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10);
break;
case 11:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11);
break;
case 12:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12);
break;
case 13:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + " " + data.substring(10, 13);
break;
case 14:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + " " + data.substring(11, 14);
break;
case 15:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + " " + data.substring(12, 15);
break;
}
} else if (data.contains(".")) {
String deci = data.substring(data.indexOf("."));
data = data.substring(0, data.indexOf("."));
switch (data.length()) {
case 4:
out = data.substring(0, 1) + " " + data.substring(1, 4) + deci;
break;
case 5:
out = data.substring(0, 2) + " " + data.substring(2, 5) + deci;
break;
case 6:
out = data.substring(0, 3) + " " + data.substring(3, 6) + deci;
break;
case 7:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + deci;
break;
case 8:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + deci;
break;
case 9:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + deci;
break;
case 10:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + deci;
break;
case 11:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + deci;
break;
case 12:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + deci;
break;
case 13:
out = data.substring(0, 1) + " " + data.substring(1, 4) + " " + data.substring(4, 7) + " " + data.substring(7, 10) + " " + data.substring(10, 13) + deci;
break;
case 14:
out = data.substring(0, 2) + " " + data.substring(2, 5) + " " + data.substring(5, 8) + " " + data.substring(8, 11) + " " + data.substring(11, 14) + deci;
break;
case 15:
out = data.substring(0, 3) + " " + data.substring(3, 6) + " " + data.substring(6, 9) + " " + data.substring(9, 12) + " " + data.substring(12, 15) + deci;
break;
}
} else {
out = data;
}
return out;
}
}
如您所见,如果您输入1250
,则文本字段中的输出将为12501 250
,而我只希望有1 250