4. Lets create the Form
Create two labels as show in the preeceding slide
The username textField use the normal textFiled
For the password use the password TextField
Create two buttons for the Login and Exit
5. Lets create the code for the buttons
Exit button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
6. Lets create the code for the buttons
Enter button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String pass = new String(jPasswordField1.getPassword());
String user = new String(jTextField1.getText());
if(user.equals("user")&& pass.equals("pass")) {
JOptionPane.showMessageDialog(this, "Login Successful");
}
else {
JOptionPane.showMessageDialog(this, "Login Failed");
}
}
7.
8. Creating a Main Menu
We want to create a main menu bar and link the menu bar with our username and
password form
Create the Main Menu form in the same package as the Login Form
9. Creating a Main Menu
We want to create a main menu bar and link the menu bar with our username and
password form
10. Creating a Main Menu
We go to the Swing Menus and select the menu Bar and by default you will get two
components added to the Menu i.e File and Edit
However, you can add other components like here l have added Help, to add help just
click on Menu and drag it to the Form and right click and select Edit text and write the
name that you want
11. Creating a Main Menu
You can add menu items that fall under File by clicking on Menu item and drag it to the File
So you can put the code under each menu item like what we have been doing in the past
12. Linking one Form to another
Going back to our Login Form we can create code so that after the correct username and
passoword have been enetered, the user is directed to the Main Menu bar
My Form for the Main Menu is: MainMenuForm
So l create a reference object that l will use to access the Form as shown
String pass = new String(jPasswordField1.getPassword());
String user = new String(jTextField1.getText());
if(user.equals("user")&& pass.equals("pass")) {
JOptionPane.showMessageDialog(this, "Login Successful");
MainMenuForm p = new MainMenuForm();
p.setVisible(rootPaneCheckingEnabled);
}
13. Task
Create a Form that enable a user to login
When the user login they should be able to capture students marks into a separate form