java账号检验规格代码 java登录验证用户名密码是否正确

java Swing应用程序中识别账号必须是数字,密码必须是以字母开头的一个登录界面。

import java.awt.GridLayout;

创新互联专注于舒城网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供舒城营销型网站建设,舒城网站制作、舒城网页设计、舒城网站官网定制、小程序定制开发服务,打造舒城网络公司原创品牌,更为您提供舒城网站排名全网营销落地服务。

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class LoginFrame extends JFrame {

private static final long serialVersionUID = 3536526127619569534L;

private JLabel nameLabel, passLabel;

private JTextField nameTextField;

private JPasswordField passwordField;

private JButton loginBtn;

public LoginFrame() {

// 设知弯置布局

this.setLayout(new GridLayout(3, 2));

nameLabel = new JLabel("用户名:");

passLabel = new JLabel("密扒陆码:");

nameTextField = new JTextField("请输入用户名");

passwordField = new JPasswordField();

loginBtn = new JButton("登录");

loginBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

// 验证是否成功的标志

boolean isSafe = true;

try {

// 临时变量

Integer tmp = Integer.valueOf(nameTextField.getText());

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "用户名必须是数字");

isSafe = false;

}

try {

// 密码的第一个字符

char start = passwordField.getPassword()[0];

if (!(start = 'a' start = 'z')

!(start = 'A' start = 'Z')) {

isSafe = false;

JOptionPane.showMessageDialog(null, "密码必须以字母开头");

}

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "请输入密码");

isSafe = false;

}

if (isSafe) {

JOptionPane.showMessageDialog(null, "登录搭此闷成功");

// 这里写验证成功后要做的事

} else {

JOptionPane.showMessageDialog(null, "登录失败");

}

}

});

this.getContentPane().add(nameLabel);

this.getContentPane().add(nameTextField);

this.getContentPane().add(passLabel);

this.getContentPane().add(passwordField);

this.getContentPane().add(loginBtn);

this.setTitle("用户登录");

this.pack();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

String lookAndFeel = UIManager.getSystemLookAndFeelClassName();

try {

UIManager.setLookAndFeel(lookAndFeel);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (UnsupportedLookAndFeelException e) {

e.printStackTrace();

}

LoginFrame login = new LoginFrame();

login.setVisible(true);

}

}

布局就用了比较简单的GridLayout,如果要实现复杂的布局可以应用多种布局加上GridBagLayout。主要看看合法性验证吧,方法肯定不止这一种的。

分享题目:java账号检验规格代码 java登录验证用户名密码是否正确
文章出自:https://www.cdcxhl.com/article38/dspggsp.html

成都网站建设公司_创新互联,为您提供动态网站域名注册微信小程序电子商务自适应网站建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作