简体   繁体   English

如何在Eclipse中验证JTextField?

[英]How to validate a JTextField in eclipse?

I want to be able to validate a number of different JTextFields in my java project. 我希望能够在我的Java项目中验证许多不同的JTextField。 One being to ensure the user enters a number into a particular field. 一种是确保用户在特定字段中输入数字。 Another problem I'm having is being able to validate a particular number, such as 1234 in a field when a submit button is pressed. 我遇到的另一个问题是能够验证特定数字,例如在按下提交按钮时在字段中输入1234。

If I have to create a method in a separate class then that fine but how would I call the method into my GUI class to use for my JTextField. 如果我必须在一个单独的类中创建一个方法,那么很好,但是我将如何将该方法调用到我的GUI类中以用于我的JTextField。

I'm quite new to Java and find some of this hard so any help is greatly appreciated. 我对Java还是很陌生,发现其中有些很难,因此对您的帮助非常感谢。

At the minute I have: 此刻我有:

import java.awt.BorderLayout;

public class CashDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtEmilysBistro;
    private JTextField textconfirmNumber;
    private String confirmNumber;
    private JButton btnSubmit;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            CashDialog dialog = new CashDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public CashDialog() {

        this.confirmNumber = confirmNumber;

        setBounds(100, 100, 526, 372);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBackground(new Color(255, 0, 153));
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            txtEmilysBistro = new JTextField();
            txtEmilysBistro.setBackground(new Color(255, 0, 153));
            txtEmilysBistro.setHorizontalAlignment(SwingConstants.CENTER);
            txtEmilysBistro.setForeground(new Color(255, 255, 255));
            txtEmilysBistro.setFont(new Font("AR ESSENCE", Font.PLAIN, 50));
            txtEmilysBistro.setEditable(false);
            txtEmilysBistro.setText("Emily's Bistro");
            txtEmilysBistro.setBounds(0, 0, 504, 57);
            contentPanel.add(txtEmilysBistro);
            txtEmilysBistro.setColumns(10);
        }

        JTextArea txtrConfirm = new JTextArea();
        txtrConfirm.setForeground(new Color(255, 255, 255));
        txtrConfirm.setBackground(new Color(255, 0, 153));
        txtrConfirm.setEditable(false);
        txtrConfirm.setFont(new Font("AR ESSENCE", Font.PLAIN, 25));
        txtrConfirm.setText("Please allow your waiter/waitress to enter         the confirmation number.");
        txtrConfirm.setBounds(54, 73, 407, 77);
        contentPanel.add(txtrConfirm);
        txtrConfirm.setLineWrap(true);

        textconfirmNumber = new JTextField();
        textconfirmNumber.setFont(new Font("AR ESSENCE", Font.PLAIN, 30));
        textconfirmNumber.setBounds(147, 148, 227, 47);
        contentPanel.add(textconfirmNumber);
        textconfirmNumber.setColumns(10);

        btnSubmit = new JButton("Submit");
        btnSubmit.setBackground(new Color(102, 51, 255));
        btnSubmit.setForeground(new Color(255, 255, 255));
        btnSubmit.setFont(new Font("AR ESSENCE", Font.PLAIN, 25));
        btnSubmit.setBounds(184, 211, 162, 29);
        contentPanel.add(btnSubmit);

    }
}

In this particular text field I only want it to accept the number 1234 in the text field. 在这个特定的文本字段中,我只希望它接受文本字段中的数字1234。

There are a few issues with your code, but that is beside the point. 您的代码有一些问题,但这不重要。 To address your question specifically here is one approach. 在这里专门解决您的问题是一种方法。 Grant it, you'd probably want to handle it differently, verify you are getting an integer instead of comparing strings, but still this is how you'd add a listener to your submit button. 授予它,您可能希望以不同的方式处理它,验证您获取的是整数而不是比较字符串,但这仍然是将侦听器添加到“提交”按钮的方式。

        btnSubmit.addActionListener(new ActionListener()
        {

                @Override
                public void actionPerformed(ActionEvent arg0)
                {
                    final String data = txtrConfirm.getText();
                    if( data.compareTo("1234") != 0 )
                    {
                        // alert the user
                    }
                    else
                    {
                        // data is good, do something with it
                    }
                }

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM