使用int值验证文本字段

时间:2013-03-21 05:56:42

标签: java swing integer jtextfield

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

任何人都可以帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

请勿使用KeyListener来尝试和过滤文字组件。

,而不是...

更新了示例

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);
            }
        });
    }

}
相关问题