Advertisement
koprobo

user

May 23rd, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.17 KB | None | 0 0
  1. mport java.util.ArrayList;
  2. import javax.swing.DefaultListModel;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Logowanie extends javax.swing.JDialog {
  6.  
  7.     /** Creates new form Logowanie */
  8.     public Logowanie(java.awt.Frame parent, boolean modal) {
  9.         super(parent, modal);
  10.         initComponents();
  11.     }
  12.  
  13.     DefaultListModel M = new DefaultListModel();
  14.     ArrayList<Uzytkownik> User;
  15.     boolean PoprawneHaslo = false;
  16.     public void UstawModel(ArrayList<Uzytkownik>U)
  17.     {
  18.         User = U;
  19.         for(int i = 0;i<U.size();i++)
  20.         {
  21.             M.addElement(U.get(i).GLogin());
  22.             jList1.setSelectedIndex(i);
  23.         }
  24.     }
  25.     @SuppressWarnings("unchecked")
  26.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  27.     private void initComponents() {
  28.  
  29.         jLabel2 = new javax.swing.JLabel();
  30.         jLabel3 = new javax.swing.JLabel();
  31.         jScrollPane1 = new javax.swing.JScrollPane();
  32.         jList1 = new javax.swing.JList();
  33.         jLabel1 = new javax.swing.JLabel();
  34.         jLabel4 = new javax.swing.JLabel();
  35.         jTextField1 = new javax.swing.JTextField();
  36.         jButton1 = new javax.swing.JButton();
  37.  
  38.         jLabel2.setText("jLabel2");
  39.  
  40.         jLabel3.setText("jLabel3");
  41.  
  42.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  43.         setBounds(new java.awt.Rectangle(600, 200, 0, 0));
  44.         addWindowListener(new java.awt.event.WindowAdapter() {
  45.             public void windowClosing(java.awt.event.WindowEvent evt) {
  46.                 formWindowClosing(evt);
  47.             }
  48.         });
  49.  
  50.         jList1.setModel(M);
  51.         jScrollPane1.setViewportView(jList1);
  52.  
  53.         jLabel1.setText("Wybierz użytkownika:");
  54.  
  55.         jLabel4.setText("Podaj hasło:");
  56.  
  57.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  58.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  59.                 jTextField1ActionPerformed(evt);
  60.             }
  61.         });
  62.  
  63.         jButton1.setText("Zaloguj");
  64.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 jButton1ActionPerformed(evt);
  67.             }
  68.         });
  69.  
  70.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  71.         getContentPane().setLayout(layout);
  72.         layout.setHorizontalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(layout.createSequentialGroup()
  75.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.                     .addGroup(layout.createSequentialGroup()
  77.                         .addContainerGap()
  78.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79.                             .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
  80.                             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
  81.                             .addComponent(jLabel4)
  82.                             .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)))
  83.                     .addGroup(layout.createSequentialGroup()
  84.                         .addGap(56, 56, 56)
  85.                         .addComponent(jButton1)))
  86.                 .addContainerGap())
  87.         );
  88.         layout.setVerticalGroup(
  89.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90.             .addGroup(layout.createSequentialGroup()
  91.                 .addContainerGap()
  92.                 .addComponent(jLabel1)
  93.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
  95.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  96.                 .addComponent(jLabel4)
  97.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  98.                 .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  99.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  100.                 .addComponent(jButton1)
  101.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  102.         );
  103.  
  104.         pack();
  105.     }// </editor-fold>                        
  106.  
  107.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  108.        
  109.     }                                          
  110.  
  111.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  112.         if(jTextField1.getText().equals(User.get(jList1.getSelectedIndex()).Haslo))
  113.         {
  114.             PoprawneHaslo = true;
  115.             this.dispose();
  116.         }else
  117.         {
  118.             JOptionPane.showMessageDialog(this, "Bledne haslo dla danego użytkownika!");
  119.         }
  120.     }                                        
  121.  
  122.     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                  
  123.         if(PoprawneHaslo == false)
  124.         {
  125.             ((Okno)this.getParent()).Zamknij();
  126.             this.dispose();
  127.         }
  128.     }                                  
  129.  
  130.     public static void main(String args[]) {
  131.         java.awt.EventQueue.invokeLater(new Runnable() {
  132.             public void run() {
  133.                 Logowanie dialog = new Logowanie(new javax.swing.JFrame(), true);
  134.                 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
  135.                     public void windowClosing(java.awt.event.WindowEvent e) {
  136.                         System.exit(0);
  137.                     }
  138.                 });
  139.                 dialog.setVisible(true);
  140. ====================================
  141. public class ModelTabeli extends AbstractTableModel{
  142.  
  143.     ArrayList<Uzytkownik>Uzytkownicy = new ArrayList<Uzytkownik>();
  144.  
  145.     public ModelTabeli(){}
  146.  
  147.     public void Ustaw (ArrayList<Uzytkownik> U)
  148.     {
  149.         this.Uzytkownicy = U;
  150.         fireTableStructureChanged();
  151.     }
  152.  
  153.  
  154.     public void Dodaj(Uzytkownik U)
  155.     {
  156.         Uzytkownicy.add(U);
  157.         fireTableRowsInserted(Uzytkownicy.size(), Uzytkownicy.size());
  158.     }
  159.  
  160.     public void Zmien(Uzytkownik U,int index)
  161.     {
  162.         Uzytkownicy.set(index, U);
  163.         fireTableRowsUpdated(index, index);
  164.     }
  165.  
  166.     public void Usun(int index)
  167.     {
  168.         Uzytkownicy.remove(index);
  169.         fireTableRowsDeleted(index,index);
  170.     }
  171.  
  172.     public int getRowCount() {
  173.        return Uzytkownicy.size();
  174.     }
  175.  
  176.     public int getColumnCount() {
  177.         return 6;
  178.     }
  179.  
  180.     public Object getValueAt(int rowIndex, int columnIndex) {
  181.         switch(columnIndex)
  182.         {
  183.             case 0:
  184.                 return rowIndex+1;
  185.             case 1:
  186.                 return Uzytkownicy.get(rowIndex).GLogin();
  187.             case 2:
  188.                 return Uzytkownicy.get(rowIndex).GImie();
  189.             case 3:
  190.                 return Uzytkownicy.get(rowIndex).GNazwisko();
  191.             case 4:
  192.                 return Uzytkownicy.get(rowIndex).GDataZalKonta();
  193.             case 5:
  194.                 return Uzytkownicy.get(rowIndex).GAdmin();
  195.             default:
  196.                 return "???";
  197.         }
  198.     }
  199.  
  200.     @Override
  201.     public String getColumnName(int columnIndex)
  202.     {
  203.         switch(columnIndex)
  204.         {
  205.             case 0:
  206.                 return "L.p.";
  207.             case 1:
  208.                 return "Login";
  209.             case 2:
  210.                 return "Imię";
  211.             case 3:
  212.                 return "Nazwisko";
  213.             case 4:
  214.                 return "Data założenia konta";
  215.             case 5:
  216.                 return "Administrator";
  217.             default:
  218.                 return "???";
  219.         }
  220. ==============================
  221. import java.io.EOFException;
  222. import java.io.File;
  223. import java.io.FileInputStream;
  224. import java.io.ObjectInputStream;
  225. import java.util.ArrayList;
  226. import java.util.Date;
  227. import javax.swing.JOptionPane;
  228.  
  229. public class Okno extends javax.swing.JFrame {
  230.     boolean Zamknac = false;
  231.     public Okno() {
  232.  
  233.         File Plik = new File("uzytkownicy.dat");
  234.         if(Plik.exists() == false)
  235.         {
  236.             M.Dodaj(new Uzytkownik("Admin","","","123456",new Date("01/06/2011"),true));
  237.         }else
  238.         {
  239.             try
  240.             {
  241.                FileInputStream Odczyt = new FileInputStream("biblioteka.txt");
  242.                ObjectInputStream Obiekty = new ObjectInputStream(Odczyt);
  243.                M.Ustaw((ArrayList<Uzytkownik>)Obiekty.readObject());
  244.                if(M.Uzytkownicy.size() == 0)M.Dodaj(new Uzytkownik("Admin","","","123456",new Date("01/06/2011"),true));
  245.                Obiekty.close();
  246.             } catch (EOFException ex)
  247.             {
  248.             }catch(Exception e)
  249.             {
  250.                 JOptionPane.showMessageDialog(this,e.getLocalizedMessage());
  251.             }
  252.         }
  253.         initComponents();
  254.         this.setVisible(false);
  255.         Logowanie L = new Logowanie(this,true);
  256.         L.UstawModel(M.Uzytkownicy);
  257.         L.show();
  258.     }
  259.  
  260.     public void Zamknij()
  261.     {
  262.         Zamknac = true;
  263.     }
  264.     @SuppressWarnings("unchecked")
  265.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  266.     private void initComponents() {
  267.  
  268.         jScrollPane1 = new javax.swing.JScrollPane();
  269.         jTable1 = new javax.swing.JTable();
  270.  
  271.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  272.         setBounds(new java.awt.Rectangle(400, 200, 0, 0));
  273.         addWindowListener(new java.awt.event.WindowAdapter() {
  274.             public void windowOpened(java.awt.event.WindowEvent evt) {
  275.                 formWindowOpened(evt);
  276.             }
  277.         });
  278.  
  279.         jTable1.setModel(M);
  280.         jScrollPane1.setViewportView(jTable1);
  281.  
  282.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  283.         getContentPane().setLayout(layout);
  284.         layout.setHorizontalGroup(
  285.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  286.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
  287.         );
  288.         layout.setVerticalGroup(
  289.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  290.             .addGroup(layout.createSequentialGroup()
  291.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  292.                 .addContainerGap(25, Short.MAX_VALUE))
  293.         );
  294.  
  295.         pack();
  296.     }// </editor-fold>                        
  297.  
  298.     private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
  299.         if(Zamknac == true)this.dispose();
  300.     }                                
  301.  
  302.     /**
  303.     * @param args the command line arguments
  304.     */
  305.     public static void main(String args[]) {
  306.         java.awt.EventQueue.invokeLater(new Runnable() {
  307.             public void run() {
  308.                 new Okno().setVisible(true);
  309.             }
  310.         });
  311.     }
  312.  
  313.     ModelTabeli M = new ModelTabeli();
  314. ======================================
  315. import java.io.Serializable;
  316. import java.util.Date;
  317.  
  318. public class Uzytkownik implements Serializable{
  319.     String Login;
  320.     String Imie;
  321.     String Nazwisko;
  322.     String Haslo;
  323.     Date DZK;
  324.     Boolean Admin;
  325.     public Uzytkownik(String Login,String Imie,String Nazwisko,String Haslo,Date DZK,Boolean Admin)
  326.     {
  327.         this.Login = Login;
  328.         this.Imie = Imie;
  329.         this.Nazwisko = Nazwisko;
  330.         this.Haslo = Haslo;
  331.         this.DZK = DZK;
  332.         this.Admin = Admin;
  333.     }
  334.     public String GLogin(){return Login;}
  335.     public String GImie(){return Imie;}
  336.     public String GNazwisko(){return Nazwisko;}
  337.     public String GDataZalKonta(){return "02-06-2011";}
  338.     public String GAdmin()
  339.     {
  340.         if(Admin == false)return "Nie";else
  341.             return "Tak";
  342.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement