Professional Documents
Culture Documents
Objectives
To create a login Form
To Create a Menu bar
Lets create the Form
Exit button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
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");
}
}
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
Creating a Main Menu
We want to create a main menu bar and link the menu bar with our username and password form
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
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
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);
}
Task