• <i id='5Mqb0'><tr id='5Mqb0'><dt id='5Mqb0'><q id='5Mqb0'><span id='5Mqb0'><b id='5Mqb0'><form id='5Mqb0'><ins id='5Mqb0'></ins><ul id='5Mqb0'></ul><sub id='5Mqb0'></sub></form><legend id='5Mqb0'></legend><bdo id='5Mqb0'><pre id='5Mqb0'><center id='5Mqb0'></center></pre></bdo></b><th id='5Mqb0'></th></span></q></dt></tr></i><div id='5Mqb0'><tfoot id='5Mqb0'></tfoot><dl id='5Mqb0'><fieldset id='5Mqb0'></fieldset></dl></div>
  • <small id='5Mqb0'></small><noframes id='5Mqb0'>

      <bdo id='5Mqb0'></bdo><ul id='5Mqb0'></ul>

    1. <legend id='5Mqb0'><style id='5Mqb0'><dir id='5Mqb0'><q id='5Mqb0'></q></dir></style></legend>

      1. <tfoot id='5Mqb0'></tfoot>

        在 Java TextField 中只接受数字和点

        Accept only numbers and a dot in Java TextField(在 Java TextField 中只接受数字和点)

            <legend id='imGZO'><style id='imGZO'><dir id='imGZO'><q id='imGZO'></q></dir></style></legend>
            • <bdo id='imGZO'></bdo><ul id='imGZO'></ul>
                <tbody id='imGZO'></tbody>
            • <small id='imGZO'></small><noframes id='imGZO'>

              • <i id='imGZO'><tr id='imGZO'><dt id='imGZO'><q id='imGZO'><span id='imGZO'><b id='imGZO'><form id='imGZO'><ins id='imGZO'></ins><ul id='imGZO'></ul><sub id='imGZO'></sub></form><legend id='imGZO'></legend><bdo id='imGZO'><pre id='imGZO'><center id='imGZO'></center></pre></bdo></b><th id='imGZO'></th></span></q></dt></tr></i><div id='imGZO'><tfoot id='imGZO'></tfoot><dl id='imGZO'><fieldset id='imGZO'></fieldset></dl></div>
              • <tfoot id='imGZO'></tfoot>

                  本文介绍了在 Java TextField 中只接受数字和点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个文本字段,我只接受来自键盘的数字,但现在我必须更改它,因为它是一个价格文本字段",我还需要接受一个点."适用于任何价格.

                  I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices.

                  我怎样才能改变它以获得我需要的东西?

                  How can I change this in order to get what I need?

                  ptoMinimoField = new JTextField();
                          ptoMinimoField.setBounds(348, 177, 167, 20);
                          contentPanel.add(ptoMinimoField);
                          ptoMinimoField.setColumns(10);
                          ptoMinimoField.addKeyListener(new KeyAdapter() {
                              public void keyTyped(KeyEvent e) {
                                  char caracter = e.getKeyChar();
                                  if (((caracter < '0') || (caracter > '9'))
                                          && (caracter != '')) {
                                      e.consume();
                                  }
                              }
                          });
                  

                  推荐答案

                  按照 Oracle 的建议,使用格式化文本字段

                  As suggested by Oracle ,Use Formatted Text Fields

                  格式化文本字段为开发人员提供了一种指定可在文本字段中键入的有效字符集的方法.

                  Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field.

                  amountFormat = NumberFormat.getNumberInstance();
                  ...
                  amountField = new JFormattedTextField(amountFormat);
                  amountField.setValue(new Double(amount));
                  amountField.setColumns(10);
                  amountField.addPropertyChangeListener("value", this);
                  

                  这篇关于在 Java TextField 中只接受数字和点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  <legend id='BaxOG'><style id='BaxOG'><dir id='BaxOG'><q id='BaxOG'></q></dir></style></legend><tfoot id='BaxOG'></tfoot>

                          <tbody id='BaxOG'></tbody>

                          <small id='BaxOG'></small><noframes id='BaxOG'>

                            <bdo id='BaxOG'></bdo><ul id='BaxOG'></ul>
                            <i id='BaxOG'><tr id='BaxOG'><dt id='BaxOG'><q id='BaxOG'><span id='BaxOG'><b id='BaxOG'><form id='BaxOG'><ins id='BaxOG'></ins><ul id='BaxOG'></ul><sub id='BaxOG'></sub></form><legend id='BaxOG'></legend><bdo id='BaxOG'><pre id='BaxOG'><center id='BaxOG'></center></pre></bdo></b><th id='BaxOG'></th></span></q></dt></tr></i><div id='BaxOG'><tfoot id='BaxOG'></tfoot><dl id='BaxOG'><fieldset id='BaxOG'></fieldset></dl></div>