You are on page 1of 18

Java Frames

Session 1
Lets create the Form

🠶 Create a New Project in Netbeans but uncheck create Main Class

🠶 Create three label for Barclays ATM, 1. Deposit, 2. Withdraw and Select Option in the
Design view
Now lets put code to control the Form

🠶 Lets start with the Exit Button, double click the Exit button in the Design View
🠶 This will take you to the Exit code button in the Source code
🠶 Now lets add this code
🠶 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

🠶 System.exit(0);
Clear button

🠶 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

🠶 jTextField1.setText(" ");
Execute Transaction button

🠶 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


try {
int choice;
int balance = 900;

choice = Integer.parseInt(jTextField1.getText());
switch(choice){
case 1: String ammt = JOptionPane.showInputDialog(this, "Enter amount to deposit");
int am = Integer.parseInt(ammt);
balance = balance + am;
JOptionPane.showMessageDialog(this, "Your new balance is "+balance);
break;
Execute Transaction button
case 2: String amount = JOptionPane.showInputDialog(this, "Enter amount to withdraw");
int m = Integer.parseInt(amount);
if(m > balance){
JOptionPane.showMessageDialog(this, " Insufficient Balance");
}
else {
balance = balance - m;
JOptionPane.showMessageDialog(this, "Your new balance is "+balance);
}
break;
default: JOptionPane.showMessageDialog(this, " Invalid Choice please try again");
}}
catch (Exception ex){
System.out.println(" Exception caught " + ex);
}
TASK 2
Create a Form that will add two numbers and show their sum
Solution

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


float num1 ;
float num2 ;
float result ;
num1 = Float.parseFloat(jTextField1.getText());
num2 = Float.parseFloat(jTextField2.getText());
result = (num1 + num2);
jTextField3.setText(String.valueOf(result));
Solution
try {
if(!jTextField1.getText().isEmpty()) {
double radius = Double.parseDouble(jTextField1.getText());
double area = pi*radius*radius;
JOptionPane.showMessageDialog(rootPane, " The area is "+area);
}
else {
JOptionPane.showMessageDialog(rootPane, "please put a valid number");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "Invalid input please put a number");
}
Solution
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
try {
if(!jTextField1.getText().isEmpty()) {
double radius = Double.parseDouble(jTextField1.getText());
double perimeter = 2*(pi*radius);
JOptionPane.showMessageDialog(rootPane, " The area is "+perimeter);
}

else {
JOptionPane.showMessageDialog(rootPane, "please put a valid number");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "Invalid input please put a number ");
}
Task 3
Solution

String text1 =jTextField1.getText();


String text2 =jTextField1.getText();

if ((text1.trim().isEmpty())||(text2.trim().isEmpty())){
JOptionPane.showMessageDialog(this, " Please input a number in the text box");
}else{}
try{
int number1 = Integer.parseInt(jTextField1.getText());
int number2 = Integer.parseInt(jTextField2.getText());
if ( number1 > number2) {
JOptionPane.showMessageDialog(this, number1 +" is greater than " + number2);
}
Solution
else if (number1 == number2){
JOptionPane.showMessageDialog(this, number1 +" is equal to " + number2);
}

else {
JOptionPane.showMessageDialog(this, number1 +" is less than " + number2);

}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(this, "Numbers Only Allowed ");
}
}
Homework

Re do the activities once again and include other shapes like calculating area of a Triangle and
Square

You might also like