hello
i am currently looking at how to place a password screen or box and the beginning and end of a program. this is to give a Sign In/ Sign Out effect.
any ideas on how i can do this
Thanks
Dave
Printable View
hello
i am currently looking at how to place a password screen or box and the beginning and end of a program. this is to give a Sign In/ Sign Out effect.
any ideas on how i can do this
Thanks
Dave
Hello Dave, now thats a rather vague question I think, what kind of program is this, are you using Swing or is it purely a console program?
// Json
a console program
Hello Dave,
Here is an example of a password prompt using Java Swing
http://javaprogrammingforums.com/images/pwdprompt.jpg
Code :import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * JavaProgrammingForums.com * Spread the word ;) */ public class Password{ // Set password here final static String pwd = "javaisthebest"; JPanel panel; JTextField myPwd = new JTextField(20); final static String BUTTONPANEL = ""; public void PasswordCheck(){ String enteredPass = myPwd.getText(); if(enteredPass.trim().equals(pwd)){ System.out.println("Correct Password!!"); } else{ System.out.println("Wrong Password. Try again."); } } public void addComponentToPane(Container pane) { JPanel panel1 = new JPanel(); panel1.add(myPwd); JButton button = new JButton("Login"); panel1.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed PasswordCheck(); } }); panel = new JPanel(new CardLayout()); panel.add(panel1, BUTTONPANEL); pane.add(panel, BorderLayout.CENTER); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Password Prompt"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. Password password = new Password(); password.addComponentToPane(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Hello again Dave!
I missed your post saying you wanted it to be a console application. How about this?
Code :import java.util.Scanner; public class PasswordConsole { /** * JavaProgrammingForums.com * Spread the word ;) */ final static String password = "javaisthebest"; public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean on = true; int count = 0; System.out.println("This is a restricted area!"); while(on){ System.out.println("Enter your password: "); String enteredPass = sc.nextLine(); if(enteredPass.equals(password)){ System.out.println("Password Correct! Welcome Dave. "); on = false; }else{ if(count == 2){ System.out.println("BOOOOOOOOOOOOOOOOOOM"); System.exit(0); } System.out.println("Wrong Password! System will self destruct after 3 failed attempts."); count++; } } } }
Example output:
Quote:
This is a restricted area!
Enter your password:
javaisthebest
Password Correct! Welcome Dave.
Quote:
This is a restricted area!
Enter your password:
javaisbad
Wrong Password! System will self destruct after 3 failed attempts.
Enter your password:
ieatmonkeys
Wrong Password! System will self destruct after 3 failed attempts.
Enter your password:
damniforgotmypass
BOOOOOOOOOOOOOOOOOOM
Thank you very much
i have 1 last question and it concerns the time recording application that was done before (see Help Code Needed thread)
i am looking to add a method that will warn the user if there has been a sudden change by 5 minutes or above.
Thanks
Dave
You might want to look into Console (Java Platform SE 6)) for reading password in console. Reading it as a string is not very secure.
// Json