Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Login Program Design > ActionListener JFrame LogInFrame LogInActionListener main

ID: 3704765 • Letter: L

Question

Login Program Design > ActionListener JFrame LogInFrame LogInActionListener mainPanel:JPanel scorePanel:JPanel userNameLabel:JLabel pwLabel:JLabel nameField:JTextField pwField:JPasswordField ok:JButton action:ActionListener actionPerformed(Event) Frame JLabel -record:File +LogInFrame0 Lab 10 Write the code for LogInFrame class. You need to have 1. Needed import statements 2. Correct class declaration 3. Declaration of all fields 4. Constructor that has only one line of code super Compile 5. If you want, you can write a testing program, similar to CardApp1. The mairn methods has only almost identical four lines. Compile and run.

Explanation / Answer

// -------------- LoginFrame.java -------------------------------

// Complete implementation is not provided here, since this is not the requirement of design Lab10.

// If you want you can find the complete implementation at the end of answer.

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class LoginFrame extends JFrame {

  

   JPanel mainPanel;

   JPanel scorePanel;

   JLabel userNameLabel;

   JLabel pwdLabel;

   JTextField nameField;

   JPasswordField pwdField;

   JButton ok;

   ActionListener action;

  

   private File record;

  

   public LoginFrame(){

       super();

       // initialize all fields here.

   }

  

   // set action = new LoginActionListner(); and add this action listener to 'ok' button.

   private class LoginActionListener implements ActionListener {

       String userName;

       String password;

      

       @Override

       public void actionPerformed(ActionEvent e) {

           userName = nameField.getText();

           password = String.copyValueOf(pwdField.getPassword());

           if(userName.equals("admin") && password.equals("admin")) {

               System.out.println("Login successful");

           }

           System.out.println("userName:" + userName +" & password:"+password);

          

       }

      

   }

}

// You can test above class by using below class

// ---------------- LoginFrameApp.java --------------------

import javax.swing.JFrame;

public class LoginFrameApp {

   public static void main(String[] args) {

       LoginFrame loginFrame = new LoginFrame();

       loginFrame.setTitle("Login Form");

       loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       loginFrame.setSize(400, 200);

       loginFrame.setVisible(true);      

   }

}

// Complete implementation of Login Form

// ----------- LoginFrame.java -------------------

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class LoginFrame extends JFrame {

  

   JPanel mainPanel;

   JPanel scorePanel;

   JLabel userNameLabel;

   JLabel pwdLabel;

   JTextField nameField;

   JPasswordField pwdField;

   JButton ok;

   ActionListener action;

  

   private File record;

  

   public LoginFrame(){

       super();

       mainPanel = new JPanel(new GridLayout(3, 1));

       userNameLabel = new JLabel("Username");

       pwdLabel = new JLabel("Password");

       nameField = new JTextField(20);

       pwdField = new JPasswordField(20);

      

       ok = new JButton("Login");

       action = new LoginActionListener();

       ok.addActionListener(action);

      

       mainPanel.add(userNameLabel);

       mainPanel.add(nameField);

       mainPanel.add(pwdLabel);

       mainPanel.add(pwdField);

       mainPanel.add(ok);

      

       add(mainPanel,BorderLayout.CENTER);

      

   }

  

   // set action = new LoginActionListner(); and add this action listener to 'ok' button.

   private class LoginActionListener implements ActionListener {

       String userName;

       String password;

      

       @Override

       public void actionPerformed(ActionEvent e) {

           userName = nameField.getText();

           password = String.copyValueOf(pwdField.getPassword());

           System.out.println("userName:" + userName +" & password:"+password);

           if(userName.equals("admin") && password.equals("admin")) {

               System.out.println("Login successful");

               JOptionPane.showMessageDialog(mainPanel, "Login Successfull..!");

           } else {

               System.out.println("Login failed");

               JOptionPane.showMessageDialog(mainPanel, "Login failed. Incorrect username or password !");

           }

          

          

       }

      

   }

}

// App class will remain same, you can add mode decoration and design to form.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote