简体   繁体   中英

How do I validate a textfield in java

public class Login extends JFrame{
    JFrame frame; //frame
    JTextField field; //to get username
    JPasswordField p; //password field
    JLabel l; //used for printing on frame
    JButton b;

    Login() {

        frame = new JFrame("Login");
        frame.setSize(350,200);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        l = new JLabel("Enter Username");
        l.setLocation(10,10);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        field = new JTextField();
        field.setColumns(15);
        field.setSize(field.getPreferredSize());

        field.setLocation(120,10);
        frame.add(field);

        l = new JLabel("Enter Password");
        l.setLocation(10,40);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        p = new JPasswordField();
        p.setColumns(15);
        p.setSize(p.getPreferredSize());
        p.setLocation(120,40);
        frame.add(p);

        b = new JButton("OK");
        b.setSize(b.getPreferredSize());
        b.setLocation(120, 80);
        frame.add(b);

        frame.setVisible(true);
    }


    private class b implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
           String str;
           str = field.getText();
           if(str.equals("")) {
               JOptionPane.showMessageDialog(null,"Please enter username");
               field.requestFocusInWindow();
           } else {

           }
        }

    }

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

the button won't functioning when I'm hit it

the button won't functioning when I'm hit it

You need to add the ActionListener to the button:

b = new JButton("OK");
b.addActionListener( new b() );

Make your class names more descriptive. "b" is not descriptive. Also, class names should start with an upper case character.

Don't use null layouts and setBounds(...). Swing was designed to be used with Layout Managers . Keep a link to the tutorial handy for Swing basics.

Take a look at How to Write an Action Listeners and How to Use Buttons, Check Boxes, and Radio Buttons .

Basically, you never register the ActionListener with your JButton

b.addActionListener(new b());

You code would be easier to read if you used meaningful variable names and followed the establishing coding conventions of the language. Have a look at Code Conventions for the Java TM Programming Language , for more details, it will make it easier for people to read your code and for you to read others

You may also want to have a read of You might want to have a read of Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? , but since you're discarded the layout manager, the use of setPreferredSize is completely pointless.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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