Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.BufferedReader;
  6. import java.io.DataInputStream;
  7. import java.io.FileInputStream;
  8. import java.io.InputStreamReader;
  9.  
  10. class Login extends JFrame implements ActionListener
  11. {
  12.  JButton SUBMIT, IP, REGISTER;
  13.  JPanel panel;
  14.  JLabel label1,label2, ipaddress;
  15.  final JTextField  text1,text2;
  16.  Font font = new Font("Arial", Font.BOLD, 15);
  17.  
  18.  String[] ips;// przez to program nie dziala
  19.  
  20.   Login()
  21.   {
  22.     JComboBox ipList = new JComboBox(ips);
  23.     ipList.setSelectedIndex(1);
  24.     ipList.setFont(font);
  25.     ipList.addActionListener(comboBox);
  26.     ipList.setEditable(isEnabled());
  27.     ipaddress = new JLabel();
  28.     ipaddress.setText("IP Address:");
  29.      
  30.     label1 = new JLabel();
  31.     label1.setText("Username:");
  32.     text1 = new JTextField(15);
  33.  
  34.     label2 = new JLabel();
  35.     label2.setText("Password:");
  36.     text2 = new JPasswordField(15);
  37.  
  38.     SUBMIT=new JButton("LOG IN");
  39.     REGISTER=new JButton("Register");
  40.     IP=new JButton("Add IP");
  41.    
  42.     panel=new JPanel(new GridLayout(0,1, 5, 5));
  43.    
  44.     panel.add(ipList);
  45.     panel.add(IP);
  46.     panel.add(label1);
  47.     panel.add(text1);
  48.     panel.add(label2);
  49.     panel.add(text2);
  50.     panel.add(SUBMIT);
  51.     panel.add(REGISTER);
  52.     add(panel,BorderLayout.CENTER);
  53.     SUBMIT.addActionListener(this);
  54.     REGISTER.addActionListener(this);
  55.     setTitle("LOGIN FORM");
  56.    
  57.     IP.addActionListener(addIP);
  58.     SUBMIT.addActionListener(submitForm);
  59.     REGISTER.addActionListener(register);
  60.     text1.addFocusListener(validation1);
  61.     text2.addFocusListener(validation2);
  62.    
  63.     readIPS();
  64.   }
  65.  
  66.   ActionListener comboBox = new ActionListener() {
  67.        public void actionPerformed(ActionEvent e)
  68.       {
  69.            JComboBox cb = (JComboBox)e.getSource();
  70.             String petName = (String)cb.getSelectedItem();
  71.       }
  72. };
  73.  
  74.   FocusListener validation1 = new FocusListener()
  75.   {
  76.  
  77.     @Override
  78.     public void focusGained(FocusEvent arg0) {
  79.         text1.setBackground(Color.white);
  80.        
  81.     }
  82.  
  83.     @Override
  84.     public void focusLost(FocusEvent arg0) {
  85.         // TODO Auto-generated method stub
  86.        
  87.     }
  88.      
  89.   };
  90.  
  91.   FocusListener validation2 = new FocusListener()
  92.   {
  93.  
  94.     @Override
  95.     public void focusGained(FocusEvent arg0) {
  96.         text2.setBackground(Color.white);
  97.        
  98.     }
  99.  
  100.     @Override
  101.     public void focusLost(FocusEvent arg0) {
  102.         // TODO Auto-generated method stub
  103.        
  104.     }
  105.   };
  106.  
  107.   ActionListener submitForm = new ActionListener() {
  108.    public void actionPerformed(ActionEvent ae)
  109.   {
  110.     JFrame frame = null;
  111.     String value1=text1.getText();
  112.     String value2=text2.getText();
  113.         if (value1.equals("lapek") && value2.equals("lapek")) {
  114.             AddIP page=new AddIP();
  115.             page.setVisible(true);
  116.             JLabel label = new JLabel("Welcome: "+value1);
  117.                 page.getContentPane().add(label);
  118.           }
  119.     else{
  120.            text1.setBackground(Color.red);
  121.            text2.setBackground(Color.red);
  122.             JOptionPane.showMessageDialog(frame,"Enter a valid username and password");
  123.           }
  124.     }
  125.   };
  126.  
  127.   ActionListener register = new ActionListener() {
  128.        public void actionPerformed(ActionEvent ae)
  129.       {
  130.        
  131.                   System.out.println("Wpisz poprawne dane i się zarejestruj");
  132.                   Register page=new Register();
  133.                   page.setSize(400, 300);
  134.                   page.setLocationRelativeTo(null);
  135.                   page.setVisible(true);
  136.       }
  137.  };
  138.  
  139.  ActionListener addIP = new ActionListener() {
  140.        public void actionPerformed(ActionEvent ae)
  141.       {
  142.        
  143.                   System.out.println("Wpisz poprawne dane i się zarejestruj");
  144.                   AddIP page = new AddIP();
  145.                   page.setSize(300, 150);
  146.                   page.setLocationRelativeTo(null);
  147.                   page.setVisible(true);
  148.       }
  149. };
  150.  
  151.     @Override
  152.     public void actionPerformed(ActionEvent arg0) {
  153.         // TODO Auto-generated method stub
  154.        
  155.     }
  156.    
  157.    
  158.     public void closeWindow()
  159.     {
  160.         this.setVisible(false);
  161.     }
  162.    
  163.     public static void readIPS()
  164.       {
  165.         try{
  166.         FileInputStream fstream = new FileInputStream("ips.txt");
  167.         DataInputStream in = new DataInputStream(fstream);
  168.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  169.         String strLine;
  170.         while ((strLine = br.readLine()) != null)   {
  171.           System.out.println (strLine);
  172.         }
  173.         in.close();
  174.         }catch (Exception e){
  175.           System.err.println("Error: " + e.getMessage());
  176.         }
  177.       }
  178.  
  179. }
  180.  class LoginDemo
  181. {
  182.   public static void main(String arg[])
  183.   {
  184.     try
  185.     {
  186.     Login frame=new Login();
  187.     frame.setSize(300,350);
  188.     frame.setVisible(true);
  189.     frame.setLocationRelativeTo(null);
  190.     }
  191.   catch(Exception e)
  192.     {JOptionPane.showMessageDialog(null, e.getMessage());}
  193.   }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement