Sinisterly
[TUT] Java, GUI password Tutorial [JAVA] - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: [TUT] Java, GUI password Tutorial [JAVA] (/Thread-TUT-Java-GUI-password-Tutorial-JAVA)



[TUT] Java, GUI password Tutorial [JAVA] - FrostByte_mybb_import5923 - 05-07-2011

Hello everyone, Iv been learning Java and i thought i would make a quick tutorial for any beginner's, this is 100% my own work not to say someone hasn't done it before as it is very simple but i did code this from scratch after watching about 10 tutorials on different stuff. This tutorial will take a look at Java Gui's and the If, Else statement. Now because it is me i couldn't make a If, Else tutorial without it being based on a password program. So let's begin.


Code:
import javax.swing.JOptionPane;

class apples {
    public static void main(String args[]){
        
        String fn = JOptionPane.showInputDialog("Password:");
                
        if (fn.equals("root")){
            JOptionPane.showMessageDialog(null, "Welcome to the CIA", "Login", JOptionPane.PLAIN_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(null, "Incorrect Login Attempt", "Login", JOptionPane.PLAIN_MESSAGE);
        }    
    }
}

Code Explained

Code:
import javax.swing.JOptionPane;
This just telling java that we are using the JOptionPane, which is just telling it we are using GUI features.


Code:
class apples {
    public static void main(String args[]){
This is just the start of the Class, you can name yours what every you want but i when i was learning i followed a tutorial and he used apple's, so i just kept it. "public static void main(String args[]){" i Dont exactly know how to explain what this does so maybe someone could help me out here but this is basically something that all java programs need to have, You may need to look that up your self.


Code:
String fn = JOptionPane.showInputDialog("Password:");
This is the First part f our GUI, it is a pop up box that has an input area and 2 button's. "String fn" is just the variable that the input is assigned to and where the input is temporarily stored.


Code:
if (fn.equals("root")){
This is the first part of our if statement, it just says if fn (User input from first box) equals "root" then. Very simple.


Code:
JOptionPane.showMessageDialog(null, "Welcome to the CIA", "Login", JOptionPane.PLAIN_MESSAGE);
This is the box that will pop if the password entered is correct. it is a standard messagebox that has a label that says "Welcome to the CIA" / a title that says "Login" and the reast just says that it's a message.


Code:
}
End the If statement.


Code:
else {
            JOptionPane.showMessageDialog(null, "Incorrect Login Attempt", "Login", JOptionPane.PLAIN_MESSAGE);
        }
This is the Else statement it just says that, if the pass isn't root then it make's the pop up box say "Incorrect Login Attempt". and title Login.


Code:
    }
}
That is just closing the other statement's, you must do this to be able to rune the program, although my java programming environment did it for me.



Thanks for reading this tutorial, Sorry if you didn't fully understand some bit's. If you would like to to explain any of it again PM me. Thanks you for reading this tutorial hope it helped you.


RE: [TUT] Java, GUI password Tutorial [JAVA] - Enc0de - 05-08-2011

Nice share keep it up the good work.


RE: [TUT] Java, GUI password Tutorial [JAVA] - FrostByte_mybb_import5923 - 05-08-2011

Thank's man, i have alot more tutorials that i will post if sage make's a programming tab