You are on page 1of 13

Java Frames

Session 4 part 2 Slides


Java Frames

Objectives
To create a login Form
To Create a Menu bar
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
Lets create the code for the buttons

 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

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

You might also like