Created
May 6, 2012 16:32
-
-
Save MgaMPKAy/2623194 to your computer and use it in GitHub Desktop.
simple LoginValidate, of no use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.*; | |
| import java.awt.event.*; | |
| import javax.swing.*; | |
| import javax.swing.event.*; | |
| import java.util.HashMap; | |
| public class LoginValidate extends JFrame { | |
| //db存放用户名和喜闻乐见的明文密码 | |
| private HashMap<String, String> db; | |
| //GUI各种界面元素 | |
| private JFrame mainFrame; | |
| private Container mainContentPane; | |
| private JTextArea resultTextArea; | |
| private JLabel usernameLabel; | |
| private JTextField usernameField; | |
| private JLabel passwordLabel; | |
| private JPasswordField passwordField; | |
| private JButton submitButton; | |
| //构造函数会建立用户数据库和GUI,并显示 | |
| LoginValidate(){ | |
| buildDB(); | |
| buildUI(); | |
| } | |
| //建立用户数据库 | |
| private void buildDB(){ | |
| //用HashMap存放用户名和密码的键值对 | |
| db = new HashMap<String, String>(); | |
| //添加两个用户做为测试 | |
| db.put("admin", "nimda"); | |
| db.put("user", "password"); | |
| } | |
| private void buildUI(){ | |
| //主窗口 | |
| mainFrame = new JFrame("Login"); | |
| mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| //设置窗口大小 | |
| mainFrame.setPreferredSize(new Dimension(400, 300)); | |
| //使用FlowLayout | |
| mainContentPane = mainFrame.getContentPane(); | |
| mainContentPane.setLayout(new FlowLayout()); | |
| //显示结果的文本框 | |
| resultTextArea = new JTextArea(); | |
| resultTextArea.setPreferredSize(new Dimension(300, 60)); | |
| resultTextArea.setEditable(false); | |
| mainContentPane.add(resultTextArea); | |
| //用户名标签 | |
| usernameLabel = new JLabel("Username:"); | |
| usernameLabel.setPreferredSize(new Dimension(300, 30)); | |
| mainContentPane.add(usernameLabel); | |
| //输入用户名 | |
| usernameField = new JTextField(); | |
| usernameField.setPreferredSize(new Dimension(300, 30)); | |
| mainContentPane.add(usernameField); | |
| //密码标签 | |
| passwordLabel = new JLabel("Password:"); | |
| passwordLabel.setPreferredSize(new Dimension(300, 30)); | |
| mainContentPane.add(passwordLabel); | |
| //输入密码 | |
| passwordField = new JPasswordField(); | |
| passwordField.setPreferredSize(new Dimension(300, 30)); | |
| mainContentPane.add(passwordField); | |
| //登录按钮 | |
| //点击调用loginAction,loginAction验证登录 | |
| submitButton = new JButton(loginAction); | |
| submitButton.setPreferredSize(new Dimension(300, 30)); | |
| mainContentPane.add(submitButton); | |
| mainFrame.pack(); | |
| //显示 | |
| mainFrame.setVisible(true); | |
| } | |
| //登录动作 | |
| Action loginAction = new AbstractAction("Login") { | |
| public void actionPerformed(ActionEvent e) { | |
| //获得各自输入 | |
| String usernameInput = usernameField.getText(); | |
| String passwordInput = passwordField.getText(); | |
| //在db中查找 | |
| String realPassword = db.get(usernameInput); | |
| //设置结果的文本框 | |
| String outputString = "Username: " + usernameInput + "\n"; | |
| if (realPassword != null && realPassword.equals(passwordInput)) { | |
| outputString += "Success"; | |
| } else { | |
| outputString += "Fail"; | |
| } | |
| resultTextArea.setText(outputString); | |
| } | |
| }; | |
| public static void main(String[] args) { | |
| LoginValidate app = new LoginValidate(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment