有没有办法强迫用户通过覆盖某些字符来提供他/她的输入。
即:
第一个屏幕:
Amount: ___.__
第二
Amount: 1__.__
...就像这样...
最后:
Amount: 1200.50
但我想确保用户按下键盘后会立即打印数字。
提前致谢。
p.s。:OS是MS Windows 9x / XP / Vista / 7。该应用程序适用于控制台。
答案 0 :(得分:3)
MaskFormatter mf1 = new MaskFormatter("#####.##");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf1 = new JFormattedTextField(mf1);
这是完整的代码
import java.awt.Container;
import java.text.ParseException;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;
public class Test {
public static void main(String args[]) throws ParseException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
MaskFormatter mf1 = new MaskFormatter("######.###");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf1 = new JFormattedTextField(mf1);
content.add(ftf1);
f.setSize(300, 100);
f.setVisible(true);
}
}
答案 1 :(得分:2)
你没有说你在做什么操作系统。为了执行高级输入终端工作,这可能很重要。
您可以花一些时间在Java中实现这样的解决方案,方法是自己捕获击键,重新生成输入线等。另一方面,您看过JLine吗?