简体   繁体   English

如何在Java中验证文本字段

[英]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: 您需要将ActionListener添加到按钮:

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

Make your class names more descriptive. 使您的班级名称更具描述性。 "b" is not descriptive. “ b”不是描述性的。 Also, class names should start with an upper case character. 同样,类名应以大写字母开头。

Don't use null layouts and setBounds(...). 不要使用null布局和setBounds(...)。 Swing was designed to be used with Layout Managers . Swing被设计为与布局管理器一起使用。 Keep a link to the tutorial handy for Swing basics. 保留指向该手册的链接,以获取Swing基础知识。

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 基本上,您永远不会向JButton注册ActionListener

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 查看Java TM编程语言的代码约定 ,以了解更多详细信息,它将使人们更容易阅读您的代码,并使您阅读其他代码更容易

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? 您可能还想读一读您可能想了解一读我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗? , but since you're discarded the layout manager, the use of setPreferredSize is completely pointless. ,但是由于您放弃了布局管理器,因此setPreferredSize的使用是完全没有意义的。

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 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 Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

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

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