OOP - Swing Lab Report
OOP - Swing Lab Report
Student Details
Name ID
[For Teachers use only: Don’t Write Anything inside this box]
Report Status
2 Objective
The aim of this lab is to demonstrate the use of Java Swing components to create a user login interface.
3 Procedure
The application was developed in the following steps:
4 Code Implementation
1 p u b l i c c l a s s Home {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 L o g i n P a g e l o g i n P a g e = new L o g i n P a g e ( ) ;
4 loginPage . s e tV i s i b le ( true ) ;
5 }
6 }
7
1 p u b l i c c l a s s L o g i n P a g e e x t e n d s JFrame i m p l e m e n t s A c t i o n L i s t e n e r {
2 p r i v a t e S t r i n g email , password ;
3 p r i v a t e J P a n e l r o o t P a n e l = new J P a n e l ( ) ;
4 p r i v a t e J L a b e l l 1 = new J L a b e l ( ) ;
5 p r i v a t e J B u t t o n l o g i n B u t t o n = new J B u t t o n ( ”LOGIN” ) ;
6 p r i v a t e J T e x t F i e l d e m a i l F i e l d = new J T e x t F i e l d ( ) ;
1
7 p r i v a t e J P a s s w o r d F i e l d p a s s F i e l d = new J P a s s w o r d F i e l d ( ) ;
8
9 p u b l i c LoginPage ( ) {
10 e m a i l F i e l d . setBounds (60 , 90 , 220 , 40) ;
11 e m a i l F i e l d . s e t F o n t ( new F o n t ( n u l l , F o n t . PLAIN , 1 6 ) ) ;
12 e m a i l F i e l d . s e t B o r d e r ( BorderFactory . createCompoundBorder (
13 emailField . getBorder () ,
14 BorderFactory . createEmptyBorder (5 , 5 , 5 , 5) ) ) ;
15 p a s s F i e l d . setBounds (60 , 150 , 220 , 40) ;
16 p a s s F i e l d . s e t F o n t ( new F o n t ( n u l l , F o n t . PLAIN , 1 6 ) ) ;
17 p a s s F i e l d . s e t B o r d e r ( BorderFactory . createCompoundBorder (
18 passField . getBorder () ,
19 BorderFactory . createEmptyBorder (5 , 5 , 5 , 5) ) ) ;
20
21 l 1 . s e t T e x t ( ” Welcome b a c k ! ” ) ;
22 l 1 . s e t F o n t ( new F o n t ( n u l l , F o n t . PLAIN , 2 4 ) ) ;
23 l 1 . setBounds (90 , 20 , 200 , 40) ;
24
29 r o o t P a n e l . s e t B a c k g r o u n d ( C o l o r . WHITE) ;
30 rootPanel . setLayout ( null ) ;
31 r o o t P a n e l . setBounds ( 0 , 0 , 360 , 360) ;
32
33 r o o t P a n e l . add ( l 1 ) ;
34 r o o t P a n e l . add ( e m a i l F i e l d ) ;
35 r o o t P a n e l . add ( p a s s F i e l d ) ;
36 r o o t P a n e l . add ( l o g i n B u t t o n ) ;
37
38 t h i s . add ( r o o t P a n e l ) ;
39 t h i s . s e t T i t l e ( ” Login ” ) ;
40 t h i s . s e t D e f a u l t C l o s e O p e r a t i o n ( EXIT ON CLOSE ) ;
41 t h i s . s e t S i z e (360 , 360) ;
42 this . setLocationRelativeTo ( null ) ;
43 t h i s . setLayout ( null ) ;
44 }
45
46 @Override
2
47 public void actionPerformed ( ActionEvent c l i c k e d ) {
48 i f ( c l i c k e d . g e t S o u r c e ( ) == l o g i n B u t t o n ) {
49 email = emailField . getText ( ) ;
50 password = S t r i n g . valueOf ( p a s s F i e l d . getPassword ( ) ) ;
51 P r o f i l e P a g e p r o f i l e P a g e = new P r o f i l e P a g e ( ) ;
52 i f ( p r o f i l e P a g e . g e t E m a i l ( ) . e q u a l s ( e m a i l ) && p r o f i l e P a g e . g e t P a s s w o r d ( ) .
e q u a l s ( password ) ) {
53 this . dispose () ;
54 profilePage . setVisible ( true ) ;
55 } else {
56 JOptionPane . showMessageDialog ( n u l l , ” I n v a l i d ’ Email ’ or ’ Password . ’ ” , ”
Wrong I n f o r m a t i o n ” , J O p t i o n P a n e . PLAIN MESSAGE ) ;
57 }
58 }
59 }
60 }
61
1 p u b l i c c l a s s P r o f i l e P a g e e x t e n d s JFrame {
2
12 public ProfilePage () {
13 image = new I m a g e I c o n ( new I m a g e I c o n ( ” image . png ” ) . g e t I m a g e ( ) .
g e t S c a l e d I n s t a n c e ( 1 0 0 , 1 1 0 , Image . SCALE DEFAULT ) ) ;
14 i m a g e L a b e l = new J L a b e l ( ) ;
15 i m a g e L a b e l . s e t I c o n ( image ) ;
16 imageLabel . setBounds (115 , 50 , 100 , 110) ;
17
3
20 nameLabel . s e t F o n t ( new F o n t ( n u l l , F o n t . PLAIN , 1 6 ) ) ;
21
26 i d L a b e l = new J L a b e l ( ” ID : ” + ” 231902018 ” ) ;
27 i d L a b e l . setBounds (60 , 250 , 200 , 20) ;
28 i d L a b e l . s e t F o n t ( new F o n t ( n u l l , F o n t . PLAIN , 1 6 ) ) ;
29
30 r o o t P a n e l . s e t B a c k g r o u n d ( C o l o r . WHITE) ;
31 rootPanel . setLayout ( null ) ;
32 r o o t P a n e l . setBounds ( 0 , 0 , 360 , 360) ;
33
34 r o o t P a n e l . add ( nameLabel ) ;
35 r o o t P a n e l . add ( d e p t L a b e l ) ;
36 r o o t P a n e l . add ( i d L a b e l ) ;
37 r o o t P a n e l . add ( i m a g e L a b e l ) ;
38
39 t h i s . add ( r o o t P a n e l ) ;
40 this . setTitle (” Profile ”) ;
41 t h i s . s e t D e f a u l t C l o s e O p e r a t i o n ( EXIT ON CLOSE ) ;
42 t h i s . s e t S i z e (360 , 360) ;
43 this . setLocationRelativeTo ( null ) ;
44 t h i s . setLayout ( null ) ;
45 }
46 public String getEmail ( ) {
47 r e t u r n email ;
48 }
49 public String getPassword ( ) {
50 r e t u r n password ;
51 }
52 }
53
5 Results/ Output
The application was tested for various scenarios including correct and incorrect user credentials. The
results were as follows:
4
• With correct credentials, the profile page was displayed, showcasing the user’s avatar and per-
sonal details such as department and ID number.
• With incorrect credentials, an error message was shown, indicating ”Invalid Email or Password”
and prompting the user to try again.
The successful login leads to a profile page that greets the user with their name and displays their
department and ID, confirming the correct entry of credentials. Conversely, an incorrect attempt
triggers a dialog box alerting the user of the wrong information, thus maintaining the security of the
user’s account.
5
6 Discussion
The development of the Java Swing application provided valuable insights into the practical appli-
cation of OOP concepts. The main objective was to create a user-friendly login system, which was
achieved through the encapsulation of user data and the implementation of event-driven programming.
The use of classes such as ‘LoginPage‘ and ‘ProfilePage‘ demonstrated encapsulation, one of the four
fundamental OOP principles. By keeping the user data private within these classes and only accessible
through methods like ‘getEmail()‘ and ‘getPassword()‘, the integrity of the user information was
maintained, showcasing the importance of data protection in software design.
Furthermore, the application’s response to user interactions, such as clicking the login button or en-
tering credentials, highlighted the concept of polymorphism. The ‘actionPerformed‘ method, which
is part of the ‘ActionListener‘ interface, was implemented to respond differently depending on the
source of the action, thus allowing for dynamic behavior.
The error handling mechanism, which displayed a message in case of incorrect credentials, empha-
sized the need for robustness in software applications. It also underlined the significance of providing
clear feedback to users, which enhances the overall user experience.
Lastly, the successful display of the profile page upon correct credential entry served as a testament
to the effective use of Java Swing components and layout managers to create intuitive graphical user
interfaces (GUIs).
7 Conclusion
In conclusion, this project successfully demonstrates the practical application of Object-Oriented Pro-
gramming (OOP) principles through the creation of a Java Swing application. The login system is
intuitive and user-friendly, ensuring secure access through proper authentication. The clear distinction
between successful logins and error messages upon incorrect credentials highlights the robustness of
the system. This lab exercise not only reinforces the understanding of Java Swing components but
also showcases the effective implementation of GUI-based user interactions, which are essential in
modern software development. Overall, the project serves as a solid foundation for further exploration
and mastery of OOP concepts in real-world applications.