Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.currencycal;
- import java.awt.GridLayout;
- import java.awt.HeadlessException;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextArea;
- public class Login extends JFrame implements ActionListener{
- private JTextArea txtUsr;
- private JPasswordField txtPass;
- private JButton btnLogin;
- private JButton newUserName;
- public Login() throws HeadlessException {
- // defining text areas and buttons
- txtUsr = new JTextArea(); //username text area
- txtPass = new JPasswordField();//password text area
- btnLogin = new JButton("Login");//button "Login"
- newUserName = new JButton("Sign UP");
- //defining ends
- // Making Layout
- GridLayout grid = new GridLayout(4,1);
- setBounds(0, 0, 200, 200);
- setLayout(grid);
- // layout made above 3 lines
- //drawing and labeling text and button on Frame
- add(new JLabel("User Name: "));
- add(txtUsr);
- add(new JLabel("Password: "));
- add(txtPass);
- add(btnLogin); //adding button on screen
- add(newUserName); // adding signup button
- //frame exits on close button
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //making login button into action
- btnLogin.addActionListener(this);
- //signup button details
- newUserName.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- new SignUp().show();
- setVisible(false);
- dispose();
- }
- });
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String textUser = txtUsr.getText();
- String textPass = txtPass.getText();
- File f1 = new File("newUser.txt");
- if(!f1.exists())
- {
- System.out.println("File not FOund");
- }
- String dataPass2 = "";
- Scanner S = null;
- try {
- S = new Scanner(f1);
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- String name3 = "";
- String pass3 = "";
- while (S.hasNextLine()) {
- dataPass2 = S.nextLine();
- String[] parts = dataPass2.split("-", 2);
- name3 = parts[0];
- pass3 = parts[1];
- //System.out.println("this is "+name3);
- //System.out.println("and"+pass3);
- //comparing with previous record
- if(textUser.equals("")&&textPass.equals(""))
- {
- JOptionPane.showMessageDialog(null,"Please insert UserName and Password");
- break;
- }
- else if(!textUser.equals(name3) && !textPass.equals(pass3))
- {
- JOptionPane.showMessageDialog(null,"wrong user name or password try again");
- break;
- }
- else
- {
- name3 = pass3 = "";
- //graphic call
- new convertCurrency().show();
- this.setVisible(false);
- this.dispose();
- }
- } };
- }
Add Comment
Please, Sign In to add comment