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

Swing Login Panel Login Enter CLEAR A Java Swing application has a login page as

ID: 3817125 • Letter: S

Question

Swing Login Panel Login Enter CLEAR A Java Swing application has a login page as shown in the Figure. Users are required to enter the correct passcode to start the application. The system uses a scramble keypad with a randomly allocated set of numbers from 0 to 9. The display shall show "Enter passcode initially, and show an asterisk for each number entered. Upon pushing the "Enter" button, the system verifies the passcode. If the passcode is correct, the system invokes a method called startApp() to start the application. Otherwise, it displays "Wrong passcode". The "Clear" button shall clear the display. Assume that the following methods are available: return the passcode public string getPasscode(){ Random rnd new Random //String passcode4digit rnd.nextInt(10) rnd.nextInt(10) rnd nextInt(10) rnd. next Int(10) String passcode4digit 7312 return passcode4digit; Start the application public void startApp(){ System.out.println("Application started"); Shuffle (Randomize) the given int array, e.g., int[] numbers {1, 2, 3, 4, 5 shuffle Array(numbers) randomize the elements

Explanation / Answer


import java.awt.Dimension;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class ButtonExample {

public static void main(String[] args) {
Dimension dim = new Dimension(100, 20);
JFrame f = new JFrame("Login");
final JTextField tf = new JTextField();
tf.setBounds(150, 40, 200, 20);
String val=new String();
  
int []arr= new int[10];
shuffleArray(arr);
  
JButton b[] = new JButton[10];
for (int i = 0; i < 10; i++) {
b[i] = new JButton(String.valueOf(i));
b[i].setBounds(i * 100 % 500, i * 20 % 40, 100, 20);
b[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String textFieldValue = tf.getText();
tf.setText("*" + textFieldValue);
  
}
});

f.add(b[i]);
}

JButton clr = new JButton("Clear");
JButton enter = new JButton("Enter");

clr.setBounds(50, 40, 100, 20);
clr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf.setText("");
//count=0;
}
});

enter.setBounds(350, 40, 100, 20);
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String textFieldValue2 = tf.getText();
if (textFieldValue2.compareTo(getPasscode()) == 0) {
startApp();
}
else
{
tf.setText("Wrong Password");
}
}
});

f.add(clr);
f.add(enter);

f.add(tf);
f.setSize(500, 100);
f.setLayout(null);
f.setVisible(true);
}

public static void startApp() {
System.out.println("Application started");
}

public static String getPasscode() {
Random rnd= new Random();
String passcode = rnd.nextInt(10)+""+rnd.nextInt(10)+""+rnd.nextInt(10)+""+rnd.nextInt(10);

return passcode;
}
static void shuffleArray(int[] ar)
{
Random rnd= new Random();
for (int i=ar.length-1;i>0;i--)
{
int index=rnd.nextInt(i+1);
int a=ar[index];
ar[index]=ar[i];
ar[i]=a;
}
}

}