【问题标题】:Validating a text field with int value [closed]验证具有 int 值的文本字段 [关闭]
【发布时间】:2013-03-21 05:56:42
【问题描述】:

我正在使用 Netbeans IDE 我想验证一个用于输入折扣百分比的文本字段。所以我想停止在 keytyped 事件中输入超过 100 的数字。

谁能帮我解决这个问题

【问题讨论】:

  • 私有 void tcs_discountKeyTyped(java.awt.event.KeyEvent evt) { String A = tcs_discount.getText();尝试{ int myint = Integer.ParseInt(A); if(!(myint >=0 && myint
  • @HashainLakshan 永远不要使用KeyListener 来尝试过滤文本组件。您无法保证KeyListeners 或调用的顺序和击键可能已经发送到您之前的字段。当用户将文本粘贴到字段中时,它们也不会被调用

标签: java swing integer jtextfield


【解决方案1】:

不要使用KeyListeners 来尝试过滤文本组件。

相反...

更新示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestSpinner02 {

    public static void main(String[] args) {
        new TestSpinner02();
    }

    public TestSpinner02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JSpinner spinner = new JSpinner();
                spinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(spinner);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
相关资源
最近更新 更多