Java 中的文本框类仅接受整数

发布于 2024-08-23 22:57:08 字数 434 浏览 1 评论 0 原文

我只想做一个只接受整数的文本框类..

我已经做了一些事情,但我认为这还不够。

有人可以帮我吗? 谢谢...

import java.awt.TextField
public class textbox extends TextField{
    private int value;

    public textbox(){
        super();

    }

    public textbox(int value){
        setDeger(value);
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {

        this.value = value;
    }
}

I just want to do a textbox class only accepts integers..

I have done something, but ı think it's not enough.

Can anyone help me, please?
Thanks...

import java.awt.TextField
public class textbox extends TextField{
    private int value;

    public textbox(){
        super();

    }

    public textbox(int value){
        setDeger(value);
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {

        this.value = value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猥︴琐丶欲为 2024-08-30 22:57:08

我认为你错过了这里的提示,用你的代码我仍然可以调用
textfield.setText("我不是数字");

I think you are missing the point here's a hint, with your code i can still call
textfield.setText("i'm not a number");

我的影子我的梦 2024-08-30 22:57:08

由于您必须使用 TextArea,因此使用 TextListener 可能会取得一些成功。添加一个侦听器,将输入的字符限制为数字。

在伪代码中,事件方法可以执行以下操作:

  1. 获取事件源
  2. 确定当前光标位置
  3. 如果当前位置的字符不是有效输入,则
  4. 删除不良字符
  5. 重置字段中的文本

JTextField 更容易做到这一点,因为您可以替换文档模型或仅使用 JFormattedTextField

Since you must use TextArea, you might have some success with a TextListener. Add a listener that restricts the characters entered to just numbers.

In pseudo code the event method could do this:

  1. get event source
  2. determine current cursor position
  3. if the character at the current position is not valid input, then
    1. remove the bad character
    2. reset the text in the field

This is easier to do with a JTextField as you can replace the document model or just use a JFormattedTextField.

原来分手还会想你 2024-08-30 22:57:08

为什么要使用文本字段?你为什么不学习Swing而不是AWT,然后你就可以使用JTextField了。实际上,您可以使用支持此要求的 JFormattedTextField。阅读 API 并点击教程链接获取示例。

Why are you using a TextField? Why don't you learn Swing instead of AWT, then you can use a JTextField. Actually you can use a JFormattedTextField which support this requirement. Read the API and follow the link to the tutorial for examples.

那伤。 2024-08-30 22:57:08

当用户尝试输入非数字数据时,它会拒绝并删除非整数的输入。

public class NumericTextField extends JTextField implements KeyListener{

    private int value;

    public NumericTextField(int value) {
        super(value+ "");
        this.addKeyListener(this);
    }
    public NumericTextField() {
        super();
        this.addKeyListener(this);
    }
    public Object getValue() {
        return value;
    }

    public void setText(int value) {
        super.setText(value + "");
        this.value = value;
    }

    @Override
    public void keyPressed(KeyEvent evt) {
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        isValidChar(arg0.getKeyChar());
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
    }

     //it is not good solution but acceptable
    private void isValidChar(char ch){
        if(this.getText().length() == 1 && this.getText().equals("-") ){
            this.setText("-");
        }
        else {
            if(!isNumeric(Character.toString(ch))){
                try{
                    this.setText(removeNonnumericChar(this.getText(), ch));
                }catch(Exception e){
                }
            }
        }
    }

    private boolean isNumeric(String text) {
        try {
            Integer.parseInt(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    //remove nonnumeric character
    private String removeNonnumericChar(String text, char ch){
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if(text.charAt(i) != ch){
                sBuilder.append(text.charAt(i));
            }
        }
        return sBuilder.toString();
    }
}

这是测试课

public class NumericTextFieldTest extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NumericTextFieldTest frame = new NumericTextFieldTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public NumericTextFieldTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 200, 150);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        NumericTextField numericText = new NumericTextField();
        numericText.setBounds(25, 27, 158, 19);
        contentPane.add(numericText);
        numericText.setColumns(10);

        JLabel lblNumericTextfield = new JLabel("Numeric textField");
        lblNumericTextfield.setBounds(37, 12, 123, 15);
        contentPane.add(lblNumericTextfield);

    }
}

When user try to enter non-numeric data, it rejects an deletes the input that is not an integer.

public class NumericTextField extends JTextField implements KeyListener{

    private int value;

    public NumericTextField(int value) {
        super(value+ "");
        this.addKeyListener(this);
    }
    public NumericTextField() {
        super();
        this.addKeyListener(this);
    }
    public Object getValue() {
        return value;
    }

    public void setText(int value) {
        super.setText(value + "");
        this.value = value;
    }

    @Override
    public void keyPressed(KeyEvent evt) {
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        isValidChar(arg0.getKeyChar());
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
    }

     //it is not good solution but acceptable
    private void isValidChar(char ch){
        if(this.getText().length() == 1 && this.getText().equals("-") ){
            this.setText("-");
        }
        else {
            if(!isNumeric(Character.toString(ch))){
                try{
                    this.setText(removeNonnumericChar(this.getText(), ch));
                }catch(Exception e){
                }
            }
        }
    }

    private boolean isNumeric(String text) {
        try {
            Integer.parseInt(text);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    //remove nonnumeric character
    private String removeNonnumericChar(String text, char ch){
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if(text.charAt(i) != ch){
                sBuilder.append(text.charAt(i));
            }
        }
        return sBuilder.toString();
    }
}

And this is the test class

public class NumericTextFieldTest extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NumericTextFieldTest frame = new NumericTextFieldTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public NumericTextFieldTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 200, 150);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        NumericTextField numericText = new NumericTextField();
        numericText.setBounds(25, 27, 158, 19);
        contentPane.add(numericText);
        numericText.setColumns(10);

        JLabel lblNumericTextfield = new JLabel("Numeric textField");
        lblNumericTextfield.setBounds(37, 12, 123, 15);
        contentPane.add(lblNumericTextfield);

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文