Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.border.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- public class MyLogin extends JFrame implements ActionListener {
- private JPanel p1, p2;
- private JLabel l1, l2, l3;
- private JTextField username;
- private JPasswordField password;
- private JButton ok, cancel;
- private JCheckBox tick;
- private String filename;
- private HashMap<String, User> table;
- public MyLogin() {
- super("Регистрация");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container container = this.getContentPane();
- p1 = new JPanel();
- p1.setBorder(new EtchedBorder());
- p1.setLayout(new GridLayout(3, 3));
- l1 = new JLabel("Потребителско име");
- p1.add(l1);
- username = new JTextField(20);
- p1.add(username);
- l2 = new JLabel("Парола");
- p1.add(l2);
- password = new JPasswordField(20);
- p1.add(password);
- l3 = new JLabel("Нов потребител");
- p1.add(
- l3);
- tick = new JCheckBox();
- p1.add(tick);
- container.add(p1, BorderLayout.CENTER);
- p2 = new JPanel();
- p2.setBorder(new EtchedBorder());
- ok = new JButton("Потвърди");
- ok.addActionListener(this);
- p2.add(ok);
- cancel = new JButton("Отмени");
- cancel.addActionListener(this);
- p2.add(cancel);
- container.add(p2, BorderLayout.PAGE_END);
- filename = "users.txt";
- table = new HashMap();
- readFile();
- this.setSize(280, 150);
- this.setVisible(true);
- }
- private void readFile() {
- FileReader fr = null;
- BufferedReader in = null;
- String line = null;
- String[] split;
- User user = null;
- try {
- fr = new FileReader(filename);
- in = new BufferedReader(fr);
- table = new HashMap();
- while((line = in.readLine()) != null) {
- split = line.split("\\t");
- user =
- new User(split[0], split[1]);
- table.put(user.getName(), user);
- }
- }
- catch (FileNotFoundException fnfe) {
- JOptionPane.showMessageDialog(this, "Файлът " + filename + " не е намерен!");
- }
- catch (IOException ioe) {
- JOptionPane.showMessageDialog(this, "Входно/изходна грешка при четене от файла " + filename);
- }
- finally {
- if(in != null) {
- try {
- in.close();
- }
- catch (IOException ioe) {
- JOptionPane.showMessageDialog(this, "Грешка при затваряне на файла " + filename);
- }
- }
- }
- }
- private void writeFile() {
- FileWriter fw = null;
- BufferedWriter bw = null;
- PrintWriter out = null;
- try {
- fw = new FileWriter(filename);
- bw = new BufferedWriter(fw);
- out = new PrintWriter(bw);
- for (Map.Entry<String, User> entry : table.entrySet())
- out.println(entry.getValue());
- }
- catch(IOException ioe) {
- JOptionPane.showMessageDialog(this, "Грешка при запис във файла " + filename);
- }
- finally {
- if(out != null) {
- out.close();
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- User user = null, find = null;
- String key = null, name = null, pass = null;
- if(source == ok) {
- name = username.getText();
- pass = new String(password.getPassword());
- if(name.length() == 0 && pass.length() == 0) {
- JOptionPane.showMessageDialog(null,
- "Въведете потребителско име и парола!","Съобщение",
- JOptionPane.INFORMATION_MESSAGE);
- }
- else {
- user = new User(name, pass);
- key = user.getName();
- if(tick.isSelected()) {
- if(table.containsKey(key)) {
- JOptionPane.showMessageDialog(null,
- "В системата съществува потребител с име " + user.getName(),
- "Съобщение", JOptionPane.INFORMATION_MESSAGE);
- }
- else {
- table.put(key, user);
- writeFile();
- new Editor(key);
- }
- }
- else {
- if(table.containsKey(key)) {
- find = table.get(key);
- if(user.getPassword().equals(find.getPassword()))
- new Editor(key);
- else
- JOptionPane.showMessageDialog(null, "Грешна парола!", "Съобщение",
- JOptionPane.INFORMATION_MESSAGE);
- }
- else
- JOptionPane.showMessageDialog(null,
- "Грешно потребителско име!", "Съобщение",
- JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- else if(source == cancel) {
- username.
- setText("");
- password.setText("");
- tick.setSelected(false);
- }
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- new MyLogin();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement