Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication12;
- import java.awt.EventQueue;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.*;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import java.io.*;
- import java.util.Scanner;
- public class LoginPage extends JFrame {
- JTextField txtuserid;
- JTextField txtpn;
- JPasswordField txtpwd;
- JButton btnLogin;
- String username, password;
- JRadioButton b1, b2;
- public LoginPage() {
- super("Log In");
- getContentPane().setLayout(null);
- JLabel lblUserid = new JLabel("User ID:");
- lblUserid.setBounds(60, 190, 112, 24);
- getContentPane().add(lblUserid);
- JLabel lblPassword = new JLabel("Password:");
- lblPassword.setBounds(60, 240, 103, 24);
- getContentPane().add(lblPassword);
- JLabel lblIP = new JLabel("Port Number:");
- lblIP.setBounds(60, 290, 92, 24);
- getContentPane().add(lblIP);
- txtuserid = new JTextField();
- txtuserid.setBounds(160, 190, 149, 30);
- getContentPane().add(txtuserid); // add textField to frame
- txtuserid.setColumns(10);
- txtpwd = new JPasswordField();
- txtpwd.setBounds(160, 240, 150, 30);
- getContentPane().add(txtpwd); // add textField to frame
- txtpwd.setColumns(10);
- txtpn = new JTextField();
- txtpn.setBounds(160, 290, 150, 30);
- getContentPane().add(txtpn); // add textField to frame
- txtpn.setColumns(10);
- txtpn.addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- char c = e.getKeyChar();
- if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
- e.consume();
- }
- }
- });
- btnLogin = new JButton("Login");
- btnLogin.setBounds(150, 400, 100, 20);
- getContentPane().add(btnLogin);
- btnLogin.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- username = txtuserid.getText();
- password = txtpwd.getText();
- if (username.equals("") || password.equals("") || txtpn.getText().equals("")) {
- JOptionPane.showMessageDialog(null, "Please fill in the blank(s).");
- } else {
- boolean access = validation();
- if (access == true) {
- if (b1.isSelected()) {
- try{
- int portnumber = Integer.parseInt(txtpn.getText());
- setVisible(false);
- }
- catch(Exception ex)
- {
- System.out.println("Unable to open AgentClient: "+ex);
- }
- } else if (b2.isSelected()) {
- try{
- int portnumber = Integer.parseInt(txtpn.getText());
- new Client(portnumber);
- setVisible(false);
- }
- catch(Exception ex)
- {
- System.out.println("Unable to open Client: "+ex);
- }
- }
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Incorrect Username & Password");
- }
- }
- }
- });
- JButton btnExit = new JButton("Exit");
- btnExit.setBounds(150, 450, 100, 20);
- getContentPane().add(btnExit);
- btnExit.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- ButtonGroup radiobutton = new ButtonGroup();
- b1 = new JRadioButton("Agent");
- b1.setBounds(75, 350, 100, 20);
- radiobutton.add(b1);
- b2 = new JRadioButton("Client");
- b2.setBounds(225, 350, 100, 20);
- radiobutton.add(b2);
- getContentPane().add(b1);
- getContentPane().add(b2);
- setResizable(false);
- setVisible(true);
- setBounds(100, 100, 400, 600);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- LoginPage loginpage = new LoginPage();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public boolean validation() {
- boolean valid = false;
- try {
- File file = new File("user.txt");
- Scanner input = new Scanner(file);
- while (input.hasNext()) {
- String id = input.next();
- String pw = input.next();
- if (id.equalsIgnoreCase(username) && pw.equals(password)) {
- valid = true;
- return valid;
- }
- }
- } catch (Exception e) {
- System.out.println("Exception thrown in validation: " + e);
- }
- return valid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement