十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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。主要看看合法性验证吧,方法肯定不止这一种的。