JAVA Practical Journal
JAVA Practical Journal
JOURNAL
NAME:
NIKITA JADHAV
ROLL NO: 17
BCA SEM 4
(TOTAL ASSIGNMENT: 60)
1
Assignment 1: Program to print "Hello World"
Code:
System.out.println("Hello World!");
2
Output:
3
Assignment 2: Program for Incremental Operator
Code:
int a=10,b=12;
System.out.println(++a);
System.out.println(a++);
System.out.println((a++)+(b--));
4
Output:
5
Assignment 3: Program for Decremental Operator
Code:
int a=10,b=12;
System.out.println(--a);
System.out.println(a--);
System.out.println((a--)+(b++));
6
Output:
7
Assignment 4: Program to Compare two numbers
Code:
int a=10,b=12;
if (a>b)
else
8
Output:
9
Assignment 5: Program to Compare three numbers
Code:
else
10
Output:
11
Assignment 6: Program to Print numbers from 1 to 10
using for loop
Code:
for(int i=1;i<=10;i++){
System.out.println(i);
12
Output:
13
Assignment 7: Program to Print even numbers between 2
to 50
Code:
for(int i=2;i<=50;i++){
if(i%2==0){
System.out.println(i);
14
Output:
15
Assignment 8: Program to Print 10 to 1 using while loop
Code:
int j=10;
while(j>=1){
System.out.println(j);
j--;
16
Output:
17
Assignment 9: Program to Print odd numbers between 50
to 100 using do while loop
Code:
int i=50;
do{
if(i%2!=0)
System.out.println(i);
i++;
}while(i<=100);
18
Output:
19
Assignment 10: Program to Print a Star Pyramid
Code:
import java.io.*;
System.out.print(" ");
System.out.print("* ");
System.out.println();
int n = 5;
printTriagle(n);
20
}
Output:
21
Assignment 11: Program to Print given pattern
Code:
import java.util.Scanner;
int rows = 3;
if(j%2 == 0)
System.out.print("-");
else
System.out.print("+");
System.out.println();
}}
22
Output:
23
Assignment 12: Program to accept numeric value as age
from user and check validation for voting
Code:
import java.util.Scanner;
int age;
age = sc.nextInt();
else
24
Output:
25
Assignment 13: Program to accept total obtained marks
and find out the grade epending upon the conditions
Code:
import java.util.Scanner;
int T;
T = sc.nextInt();
26
else if (T >=50 && T <=69)
else if (T <=35)
else
27
Output:
28
Assignment 14: Program to accept 3 subject marks from the
user and calculate the Totalmarks, Percentage and Average
Code:
import java.util.Scanner;
eng=op.nextFloat();
subjects:"); math=op.nextFloat();
subjects:"); comp=op.nextFloat();
(total / 3.0);
29
/* Print all results */
System.out.println("Percentage = "+percentage);
Output:
30
Assignment 15: Program to show String function in JAVA
Code:
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
31
Output:
32
Assignment 16: Program to accept 2 Strings from the user
and compare which string is greater with respect to the
length
Code:
import java.util.Scanner;
s1 = in.nextLine();
string"); s2 = in.nextLine();
33
System.out.println("The first string is smaller than the second.");
else {
34
Output:
35
Assignment 17: Program to accept string from user to check
if it is Palindrome or not using inbult finction
Code:
import java.util.Scanner;
java.lang.Exception {
if (str.equals(reverse))
System.out.println("String is palindrome");
else
36
Output:
37
Assignment 18: program to check Password and Confirm
Password are same or not
Code:
import java.util.Scanner;
if(myStr1.equals(myStr2))
else
38
Output:
39
Assignment 19: Program to accept username from the user
and check whether its length is between 8 to 15 characters
Code:
import java.util.Scanner;
else{
40
Output:
41
Assignment 20: Program to accept word from user and
convert it to uppercase
Code:
import java.util.Scanner;
String s1upper=s1.toUpperCase();
42
Output:
43
Assignment 21: Program to accept First name, Middle name
and Last name from the user and display Full name
Code:
import java.util.Scanner;
s1 = in.nextLine();
name"); s2 = in.nextLine();
s3 = in.nextLine();
44
Output:
45
Assignment 22: Program to accept "India is my country, I
love India" and replace "India" with "America"
Code:
import java.util.Scanner;
String replaceString=s1.replace("India","America");
System.out.println(replaceString);
46
Output:
47
Assignment 23: Program to perform Addition of 2 numbers
using parameterised function
Code:
import java.util.Scanner;
int a, b;
a = sc.nextInt();
b = sc.nextInt();
48
Output:
49
Assignment 24: Program to create class example
Code:
Computer() {
void computer_method() {
c.computer_method();
l.laptop_method();
class Laptop {
Laptop() {
void laptop_method() {
50
Output:
51
Assignment 25: Program to accept number from user and
find its Factorial
Code:
import java.util.Scanner;
int i,fact=1;
int number;
number = s.nextInt();
for(i=1;i<=number;i++)
fact=fact*i;
52
Output:
53
Assignment 26: Program to accept full name from user
Code:
import java.util.Scanner;
String s1;
s1 = in.nextLine();
54
Output:
55
Assignment 27: Program for college to enroll 10 students
data for enqiry process
Code:
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
public Student() {
name = initName;
stu_id = initId;
score = initScore;
new Scanner(System.in);
56
Student stu = new Student();
stu.name = in.next();
stu.stu_id = in.next();
stu.score = in.nextInt();
max.name = stu.name;
max.stu_id = stu.stu_id;
max.score = stu.score;
min.name = stu.name;
min.stu_id = stu.stu_id;
min.score = stu.score;
min.stu_id); in.close();
57
Output:
58
Assignment 28: Program to do function overloading which
accepts radius and calculate the area of the circle
Code:
import java.util.Scanner;
import java.lang.Math.*;
59
if (choice == 1) {
obj.circleCalculate(radius);
} else if (choice == 2) {
int x = 2;
obj.circleCalculate(radius, x);
System.out.print(":)");
else{
System.out.print("Chose again\n\n");
main(null);
input.close();
60
Output:
61
Assignment 29: Program to perform function overloading,
Take students subjects
Code:
import java.util.*;
class Overloading {
Scanner(System.in); System.out.println("Please
2.BCA");
62
if (choice == 1) {
obj.admission(name, total);
} else {
input.close();
63
Output:
64
Assignment 30: Program to accept number from user and
check whether its prime or not
Code:
import java.util.Scanner;
int n = s.nextInt();
if (isPrime(n)) {
number"); } else {
number"); } }
if (n <= 1) {
return false;
if (n % i == 0) {
return false;
}}
return true; }
65
Output:
66
Assignment 31: Program to accept number from user and
find out its factorial
Code:
import java.util.Scanner;
class FactorialExample2
if (n == 0)
return 1;
else
return(n * factorial(n-1));
int number;
number = s.nextInt();
int i,fact=1;
fact = factorial(number);
67
Output:
68
Assignment 32: Program for interface named as rate
calculation use an abstract fun
Code:
class Bank1{
Bank b;
b=new SBI();
69
Output:
70
Assignment 33: Program to accept Length and Breadth of
rectangle using class
Code:
import java.util.Scanner;
obj.RectangleLength(length, bredth);
71
Output:
72
Assignment 34: Program to perform Multithreading
Example
Code:
{ private Thread t;
threadName = name;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
73
}
if (t == null) {
t.start ();
R1.start();
2"); R2.start();
74
Output:
75
Assignment 35: Program to accept number from the user to
get all even, odd and check whether entered number is
even or odd integers
Code:
import java.util.Scanner;
even.start();
odd.start();
userInput.start();
@Override
System.out.println(i);
76
}
@Override
System.out.println(i);
@Override
int n = input.nextInt();
if (n % 2 == 0)
else
77
Output:
78
Assignment 36: Program to create and run the following
threads Print "Hi" 10 times and "Bye" 20 times
Code:
int remainder;
OddEvenRunnable3(int remainder)
this.remainder=remainder;
@Override
{ synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
79
e.printStackTrace();
number++;
lock.notifyAll();
t2=new Thread(evenRunnable,"Bye\nBye");
t1.start();
t2.start();
80
Output:
81
Assignment 37: Program to accept Starting and Ending
number from user and print Even and Odd numbers
Code:
import java.util.Scanner;
int i;
for(i=start;i<=end;i++){
if(i%2==0){
System.out.println("Even "+i);
else{
System.out.println("Odd "+i);
82
Output:
83
Assignment 38: Program to plot a button in AWT
Code:
import java.awt.*;
b1.setBounds(80,200,80,50);
f1.add(b1);
f1.setSize(500,500);
f1.setLayout(null);
f1.setVisible(true);
84
Output:
85
Assignment 39: Program to Plot a label, button and
textfield through AWT
Code:
import java.awt.*;
import java.awt.event.*;
TextField tf1,tf2;
Label tf3;
Button b1,b2;
TextFieldExample(){
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new Label();
tf3.setBounds(50,150,150,20);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
86
setSize(300,300);
setLayout(null);
setVisible(true);
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
String result=String.valueOf(c);
tf3.setText(result);
new TextFieldExample();
87
Output:
88
Assignment 40: Program to draw a smiley face using applet
Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
@Override
g.setColor(Color.YELLOW);
g.fillOval(50,50,200,200);
g.drawOval(50,50,200,200);
g.setColor(Color.red);
g.fillOval(70,90,50,50);
g.drawOval(70,90,50,50);
g.setColor(Color.red);
g.fillOval(180,90,50,50);
g.drawOval(180,90,50,50);
g.setColor(Color.black);
89
Output:
90
Assignment 41: Program to plot button through AWT with
a frame
Code:
import java.awt.*;
SimpleExample(){
b.setBounds(50,50,50,50);
add(b);
setSize(500,300);
setLayout(new FlowLayout());
setVisible(true);
91
Output:
92
Assignment 42: Program to accept name from the user and
show welcome person name
Code:
import java.awt.*;
import java.awt.event.*;
TextField tf1;
Label tf3;
Button b1;
TextFieldExample(){
tf1=new TextField();
tf1.setBounds(50,50,160,20);
tf3=new Label();
tf3.setBounds(50,150,160,20);
b1=new Button("Show");
b1.setBounds(50,200,80,80);
b1.addActionListener(this);
add(tf1);add(tf3);add(b1);
setSize(400,400);
setLayout(null);
setVisible(true);
String s1=tf1.getText();
93
if(e.getSource()==b1){
tf3.setText("Welcome "+s1);
new TextFieldExample();
94
Output:
95
Assignment 43:Program to create a login form
Code:
import java.awt.*;
import java.awt.event.*;
TextField name,pass;
Button b1,b2;
MyLoginWindow()
setLayout(new FlowLayout());
this.setLayout(null);
TextField(20);
pass.setEchoChar('#');
b1=new Button("submit");
b2=new Button("cancel");
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
this.add(b1);
96
this.add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(200,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
ml.setVisible(true);
ml.setSize(400,400);
97
Output:
98
Assignment 44: Program to create window with 3 checkbox
labeled Red, Green and Blue when checked on one the
background color changes respectively
Code:
import java.awt.*;
import java.awt.event.*;
CheckboxExample(){
label.setAlignment(Label.CENTER);
label.setSize(400,100);
checkbox1.setBounds(100,100, 50,50);
checkbox2.setBounds(100,150, 50,50);
checkbox3.setBounds(100,200, 50,50);
checkbox1.addItemListener(new ItemListener() {
if(e.getStateChange()==1){
f.setBackground(Color.RED);
99
}
});
checkbox2.addItemListener(new ItemListener() {
if(e.getStateChange()==1){
f.setBackground(Color.GREEN);
});
checkbox3.addItemListener(new ItemListener() {
if(e.getStateChange()==1){
f.setBackground(Color.BLUE);
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new CheckboxExample();
100
Output:
101
Assignment 45: Program to create window with 3
radiobuttons labeled Red, Green and Blue when checked on
one the background color changes respectively
Code:
import java.awt.*;
import java.awt.event.*;
Frame f;
backcolorradio()
radioBtn2.setBounds(20,140, 70,50);
f.add(radioBtn1);
102
f.add(radioBtn2);
f.add(radioBtn3);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
radioBtn1.addItemListener(new ItemListener() {
f.setBackground(Color.RED);
l1.setBackground(Color.RED);
radioBtn1.setBackground(Color.RED);
radioBtn2.setBackground(Color.RED);
radioBtn3.setBackground(Color.RED);
});
radioBtn2.addItemListener(new ItemListener() {
f.setBackground(Color.GREEN);
l1.setBackground(Color.GREEN);
radioBtn1.setBackground(Color.GREEN);
radioBtn2.setBackground(Color.GREEN);
radioBtn3.setBackground(Color.GREEN);
});
103
radioBtn3.addItemListener(new ItemListener() {
f.setBackground(Color.BLUE);
l1.setBackground(Color.BLUE);
radioBtn1.setBackground(Color.BLUE);
radioBtn2.setBackground(Color.BLUE);
radioBtn3.setBackground(Color.BLUE);
});
new backcolorradio();
104
Output:
105
Assignment 46: Program to change button label from OK to
KO when clicked and vise versa
Code:
import java.awt.*;
import java.awt.event.*;
b=new Button("OK");
b.setBounds(50,100,60,40);
b.addActionListener(new ActionListener(){
if(b.getLabel().equals("OK")){
b.setLabel("KO"); }
else{
b.setLabel("OK"); }
});
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
106
Output:
107
Assignment 47 Program to create window with 3 Scrollbars
labeled Red, Green and Blue when checked on one the
background color changes respectively
Code:
import java.awt.*;
import java.awt.event.*;
class ScrollbarExample{
ScrollbarExample(){
l1.setBounds(10, 40,200,40);
r.setBounds(50,100, 50,100);
g.setBounds(110,100, 50,100);
b.setBounds(170,100, 50,100);
f.add(l1);
f.add(r);
f.add(g);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
108
r.addAdjustmentListener(new AdjustmentListener() {
{ f.setBackground(Color.RED);
l1.setBackground(Color.RED);
l1.setText("Red");
});
g.addAdjustmentListener(new AdjustmentListener() {
f.setBackground(Color.GREEN);
l1.setBackground(Color.GREEN);
l1.setText("Green");
});
b.addAdjustmentListener(new AdjustmentListener() {
f.setBackground(Color.BLUE);
l1.setBackground(Color.BLUE);
l1.setText("Blue"); }
});
new ScrollbarExample();
109
Output:
110
Assignment 48: Program to create 2 buttons and one
textfield on clicking button label will be displaied in the
textfield
Code:
import java.awt.*;
import java.awt.event.*;
l1.setBounds(10, 40,200,40);
b1.setBounds(50,100,60,40);
b1.addActionListener(new ActionListener(){
l1.setText(b1.getLabel());
});
b2.setBounds(150,100,60,40);
b2.addActionListener(new ActionListener(){
l1.setText(b2.getLabel());
111
}
});
f.add(l1);
f.add(b1);
f.add(b2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
112
Output:
113
Assignment 49: Program to accept text formatting using
combo buttons
Code:
import java.awt.*;
import java.awt.event.*;
ChoiceExample(){
label.setAlignment(Label.CENTER);
label.setSize(400,100);
b.setBounds(200,100,50,20);
c.setBounds(100,100, 75,75);
c.add("2021");
c.add("2020");
c.add("2019");
c.add("2018");
c.add("2017");
f.add(c);f.add(label); f.add(b);
f.setSize(400,400);
114
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
label.setText(data);
});
new ChoiceExample();
115
Output:
116
Assignment 50: Program to draw foll layout with help of
AWT
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
ActionListener {
Checkbox ch1,ch2;
Button b;
Label l1;
Frame f;
Fontfamily()
f= new Frame();
f.add(l1);
117
ch1.setBounds(100,150, 80, 50);
f.add(ch1);
f.add(ch2);
b=new Button("Submit");
f.add(b);
b.addActionListener(this);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
@Override
118
public void actionPerformed(ActionEvent e )
if(e.getSource().equals(b))
if(ch1.getState())
l1.setForeground(Color.red);
if(ch2.getState())
l1.setFont(serif);
119
Output:
120
Assignment 51: Program to find lenght of String entered in
textfield
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
ActionListener {
JFrame f;
JButton b3;
JLabel l2;
JTextField t1;
StringlenAWT()
l1.setBounds(100,20,200,40);
f.add(l1);
t1=new JTextField();
t1.setBounds(200,20,180,40);
f.add(t1);
121
b3=new JButton("Length");
b3.setBounds(340,80,100,40);
f.add(b3);
b3.addActionListener((ActionListener) this);
l2.setBounds(100,120,200,40);
f.add(l2);
f.setSize(800,800);
f.setLayout(null);
f.setVisible(true); }
@Override
if(e.getSource().equals(b3)){
String s=t1.getText();
l2.setText("Length is "+s.length()); }
new StringlenAWT(); }
122
Output:
123
Assignment 52: Program to create a Calculator
Code:
import java.awt.*;
import java.awt.event.*;
ActionListener {
Frame f;
Label l1,l2,l3,l4;
TextField t1,t2;
Calculator()
f=new Frame();
l1.setBounds(20,50,150,40);
f.add(l1);
f.add(l2);
124
t1=new TextField();
f.add(t1);
t2=new TextField();
f.add(t2);
b1=new Button("+");
f.add(b1);
b1.addActionListener(this);
b2=new Button("-");
f.add(b2);
b2.addActionListener(this);
b3=new Button("*");
f.add(b3);
b3.addActionListener(this);
b4=new Button("/");
125
f.add(b4);
b4.addActionListener(this);
f.add(l3);
l4=new Label();
f.add(l4);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
126
public static void main(String args[])
@Override
if(e.getSource().equals(b1))
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int c=a+b;
l4.setText(String.valueOf(c));
if(e.getSource().equals(b2))
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int c=a-b;
l4.setText(String.valueOf(c));
127
}
if(e.getSource().equals(b3))
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int c=a*b;
l4.setText(String.valueOf(c));
if(e.getSource().equals(b4))
int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
int c=a/b;
l4.setText(String.valueOf(c));
128
Output:
129
Assignment 53: Program to create a button in Jswing
Code:
import javax.swing.*;
b.setBounds(50,100,95,30);
b.setLocation(100, 100);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
130
output:
131
Assignment 54: Program to create button in jswing using
Constructor
Code:
import javax.swing.*;
JFrame f;
JButton b;
ButtonExample(){
b.setBounds(50,100,95,30);
b.setLocation(100, 100);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
132
Output:
133
Assignment 55: Program to do string functions
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
ActionListener{ JFrame f;
JButton b1,b2,b3,b4,b5;
JLabel l2;
JTextField t1;
Text()
l1.setBounds(100,20,200,40);
f.add(l1);
t1=new JTextField();
t1.setBounds(200,20,180,40);
f.add(t1);
b1=new JButton("Show");
b1.setBounds(100,80,100,40);
134
f.add(b1);
b1.addActionListener((ActionListener) this);
b2=new JButton("Clear");
b2.setBounds(220,80,100,40);
f.add(b2);
b2.addActionListener((ActionListener) this);
b3=new JButton("Length");
b3.setBounds(340,80,100,40);
f.add(b3);
b3.addActionListener((ActionListener) this);
b4=new JButton("Palindrome");
b4.setBounds(460,80,100,40);
f.add(b4);
b4.addActionListener((ActionListener) this);
b5=new JButton("Vowel");
b5.setBounds(580,80,100,40);
f.add(b5);
135
b5.addActionListener((ActionListener) this);
l2.setBounds(100,120,200,40);
f.add(l2);
f.setSize(800,800);
f.setLayout(null);
f.setVisible(true);
new Text();
@Override
if(e.getSource().equals(b1)){
else if(e.getSource().equals(b2)){
t1.setText("");
136
else if(e.getSource().equals(b3)){
String s=t1.getText();
l2.setText("Length is "+s.length()); }
else if(e.getSource().equals(b4)){
String str=t1.getText();
(str.equals(reverse)) {
l2.setText("Word is Palindrome"); }
else {
else if(e.getSource().equals(b5)){
String v=t1.getText();
int count = 0;
char ch = v.charAt(i);
count ++;
137
Output:
138
Assignment 56: Program to display radio buttons
to select your gender
Code:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.NullPointerException;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
JFrame f;
JLabel l2;
JRadioButton r1,r2;
Gender()
l2.setBounds(90,120,200,40);
f.add(l2);
139
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
r1.addItemListener(new ItemListener() {
});
r2.addItemListener(new ItemListener() {
});
new Gender();
140
Output:
141
Assignment 57: Program to print given pattern
Code:
int flag = 1;
System.out.print(flag);
System.out.println();
if(flag == 0) {
flag=1;
else {
flag=0;
142
Output:
143
Assignment 58: Program to print given pattern
Code:
System.out.print(" "+i);
System.out.println();
144
Output:
145
Assignment 59: Program to print Hello World! an name of
user in AWT with white background
Code:
import java.awt.*;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.*;
f.getContentPane().setBackground(Color.BLACK);
l1.setForeground(Color.red);
l1.setBounds(100,80,200,40);
l2.setForeground(Color.green);
l2.setBounds(200,80,200,40);
}}
146
Output:
147
Assignment 60: Program to make a Login window in Jswing
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
LoginFrame() {
setLayoutManager();
setLocationAndSize();
addComponentsToContainer();
148
addActionEvent();
container.setLayout(null);
loginButton.setForeground(Color.WHITE);
loginButton.setBackground(Color.GREEN);
resetButton.setForeground(Color.WHITE);
resetButton.setBackground(Color.GREEN);
container.add(userLabel);
container.add(passwordLabel);
149
container.add(userTextField);
container.add(passwordField);
container.add(showPassword);
container.add(loginButton);
container.add(resetButton);
container.setBackground(Color.WHITE);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
showPassword.addActionListener(this);
@Override
if (e.getSource() == loginButton) {
String userText;
String pwdText;
userText = userTextField.getText();
pwdText = passwordField.getText();
} else {
150
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
if (e.getSource() == resetButton) {
userTextField.setText("");
passwordField.setText("");
if (e.getSource() == showPassword) {
if (showPassword.isSelected()) {
passwordField.setEchoChar((char) 0);
} else {
passwordField.setEchoChar('*');
frame.setTitle("Login Form");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false); } }
151
Output:
152