JTextField:将小数点逗号重新映射到点

时间:2012-08-10 12:06:40

标签: java swing events keyboard jtextfield

我想将小数点逗号(德语键盘)重新映射到某些JTextField的点。我已经尝试了getInputMap()getActionMap,但到目前为止还没有任何效果。到目前为止我得到的最好结果是逗号根本不起作用。

我认为应该有一些使用输入地图的方式,但是如何?

下面是一个小示例程序,有人可以给我一个关于在评论位置填写什么的提示吗?或者这完全是错的吗?

import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class KeyTest extends JFrame {

    private JTextField textField;

    public KeyTest() {
        textField = new JTextField();
        add(textField);

        InputMap map = textField.getInputMap();
        map.put(KeyStroke.getKeyStroke(','), /* what to do here? */);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                KeyTest test = new KeyTest();
                test.pack();
                test.setVisible(true);
            }
        });
    }
}

修改 也许我对我的问题不够清楚。正常数字没有问题。我只是想在输入日期时让用户输入更方便。这些格式为德国"DD.MM.YYYY"格式。这意味着,您不能仅使用数字键盘输入日期,因为我们没有小数点,而是逗号。这就是为什么我想在用于输入日期的文本字段中用逗号替换逗号。

纯文本和数字的所有输入字段都已正常工作,甚至日期输入都可以正常工作。我只是想让打字变得更容易。就在昨天,我注意到Libreoffice中的类似功能,其中数字小键盘上的逗号(我有一个德语键盘)在数字单元格中自动替换(我的语言环境设置为英语),但仅限于包含数值的单元格。输入时也会发生这种情况。

5 个答案:

答案 0 :(得分:3)

正如@MKorbel建议的那样,您可以使用JFormattedTextField并提供合适的Locale

JFormattedTextField field = new JFormattedTextField(
    new NumberFormatter(NumberFormat.getNumberInstance(Locale.UK)));
field.setValue(new Double(123.45));

答案 1 :(得分:3)

  • 必须使用JFormattedTextFieldJSpinner使用数字格式化程序,而不是简单的JTextField,我没有使用小数点分隔符而不设置Locale

  • 第二种使用DocumentFilter而不是InputMask的方式,用于过滤不需要的whitespacenon_numeric字符,并将点更改为所需的小数分离器

  • 通过内部使用DocumentFilterJFormattedTextFieldJSpinner Java输出来保持dot作为ASCII小数点分隔符,只有区别在视图和模型(Swing Document)之间与DocumentFilter进行比较,返回更改的分组(空格)和分隔符

答案 2 :(得分:2)

您要找的是“NumberFormat”(http://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html)

示例:

NumberFormat english = NumberFormat.getInstance(Locale.ENGLISH); 
Number n = english.parse("0.123");   //english notation
NumberFormat german = NumberFormat.getInstance(Locale.GERMAN);
System.out.println(german.format(n.floatValue())); //output 0,123 (german notation)

答案 3 :(得分:1)

使用以下

textField.addKeyListener(new KeyListener(){

  public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
     if (c== ',')
       e.setKeyChar('.');
  }
});

答案 4 :(得分:0)

我找到了另一种管理逗号/点问题的方法,这个问题很简单。 这个解决方案应该适用于全世界,并且不依赖于键盘的类型和 你的LOCALE设置。

我正在使用JFormattedTextField获取正确的Localized输出,并将“.replace(',','。')”添加到parseDouble()函数中,如下例所示。

private void ConvertMilesToKilometers(){

 Double inputNumber = 0.0;

 // to format the output string
 DecimalFormat df = new DecimalFormat("#,###.##");

 // another way will be to use LOCALE 
 // DecimalFormat df = (DecimalFormat)NumberFormat.getNumberInstance(Locale.GERMANY);

 // get user input
 try {
     inputNumber = Double.parseDouble(milesTextField.getText().replace(',', '.'));
 } catch (Exception e) {
     JOptionPane.showMessageDialog(this, "please enter a valid number!", null, JOptionPane.ERROR_MESSAGE);
 }

    // calculate and give the answer 
    String answer = df.format(inputNumber * 1.609344f);
    this.KilometersLabel.setText(answer + " Kilometers."); 

    // clear the input field 
    this.milesTextField.setText("");
}

我希望这会有所帮助。 来自德国的问候。