// return the passcode public String getPasscode(){ //Random rnd = new Random();
ID: 3815368 • Letter: #
Question
// return the passcode
public String getPasscode(){
//Random rnd = new Random();
//String passcode4digit = rnd.nextInt(10) + “” + rnd.nextInt(10) + “” + // rnd.nextInt(10) + “” + rnd.nextInt(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};
// shuffleArray(numbers);
// randomize the elements
public void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
Swing LoginPanel CLEAR Login EnterExplanation / Answer
import javax.swing.*;
import java.awt.*;
class NextPage extends JFrame
{
NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1;
final JTextField text1;
Login()
{
label1 = new JLabel();
label1.setText("Password:");
text1 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
Random rnd = new Random();
String passcode4digit = rnd.nextInt(10) + “” + rnd.nextInt(10) + “” + // rnd.nextInt(10) + “” + rnd.nextInt(10);
if (value1.equals("passcode4digit") ) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.