Tạo Giao Diện đăng Ký Tài Khoản Trong Java Swing - Tìm ở đây

Đề bài: Viết chương trình tạo một cửa sổ cho phép đăng ký thông tin người dùng gồm tên người dùng, mật khẩu và quyền hạn truy cập. Tạo một danh sách lưu trữ những người dùng đăng ký. Yêu cầu kiến thức:

  • Kế thừa và triển khai các phương thức đã được đề ra ở các lớp cha
  • Phân tích và thiết kế các đối tượng trên giao diện là các components
  • Hiểu rõ bản chất cách hoạt động các đối tượng

Cấu trúc thư mục: src |——buildUI    |——Account.java    |——SingUp.java |——usingUI    |——MainClass.java Code tham khảo dưới đây được viết trên JDK ver 8.x: File Account.java:

package buildUI; public class Account { // Thuoc tinh private String userName; private String password; private String access; // Ham khoi tao khong tham so public Account(){ userName=""; password=""; access=""; } // Ham khoi tao co doi so public Account(String userName, String password, String access){ this.userName=userName; this.password=password; this.access=access; } // Cac ham getter va setter public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAccess() { return access; } public void setAccess(String access) { this.access = access; } }

File SingUp.java:

package buildUI; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; public class SingUp extends JFrame implements ActionListener { // Khai bao cac component JLabel lbUser=new JLabel("Username: "); JLabel lbPassword=new JLabel("Password: "); JTextField txtUser=new JTextField(); JPasswordField txtPassword=new JPasswordField(); JLabel lbAccess=new JLabel("Access right: "); JComboBox cbAccess=new JComboBox(); JButton btnSave=new JButton("Save"); JButton btnCancel=new JButton("Cancel"); // Luu danh sach nguoi dung ArrayList listUser=new ArrayList(); // Ham khoi tao public SingUp(String title){ super(title); // Tao container Container con=this.getContentPane(); // Tao panel 1 chua username, pass, access JPanel panel1=new JPanel(); panel1.setLayout(new GridLayout(3, 2)); panel1.add(lbUser); panel1.add(txtUser); panel1.add(lbPassword); panel1.add(txtPassword); // Tao danh sach quyen truy cap cbAccess.addItem("Admin"); cbAccess.addItem("Teacher"); cbAccess.addItem("Student"); panel1.add(lbAccess); panel1.add(cbAccess); // Tao panel 2 chua buttun save, access JPanel panel2=new JPanel(); panel2.setLayout(new FlowLayout()); panel2.add(btnSave); panel2.add(btnCancel); // Dat phuong thuc trang thai btnSave.addActionListener(this); btnCancel.addActionListener(this); // Dat panel 1 o vung trung tam con.add(panel1); // Dat panel 2 vao vung South con.add(panel2, "South"); this.pack(); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // Neu click btn save thi lay du lieu cua cac truong if(e.getActionCommand().equals("Save")) { String userName = txtUser.getText(); String passWord = txtPassword.getText(); String access = (String) cbAccess.getSelectedItem(); // Tao nguoi dung moi Account newUser = new Account(userName, passWord, access); // Them nguoi dung vao danh sach listUser.add(newUser); } else { // Neu click Cancel thi thoat chuong trinh this.dispose(); } } }

File MainClass.java:

package usingUI; import buildUI.SingUp; public class MainClass { public static void main(String[] args) { // Tao giao dien cho phep dang ky SingUp singUp=new SingUp("Sign Up"); } }

Kết quả chương trình: Kết luận:

  • Bạn có thể tham khảo thêm khóa học lập trình Java từ cơ bản đến nâng cao. Xem tại đây
  • Bạn có thể tham khảo thêm khóa học lập trình C từ cơ bản đến nâng cao. Xem tại đây
  • Bạn có thể tham khảo thêm khóa học Thành thạo lập trình C#. Xem tại đây
  • Bạn có thể tham khảo thêm khóa học Ôn tập OOP cơ bản trong Java. Xem tại đây

Từ khóa » Cách đăng Nhập Java