Technotaught

Registration Form in Windows Form Using Swing in Java

Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a huge set of components that provide a high level of customization and provide smart functionalities and is used to create a window-based applications. Java swing components are lightweight, platform-independent, provide powerful components like tables, scroll panels, buttons, lists, color chooser, etc. In this article, we’ll see how to make a Registration Form in Windows Form Using Swing in Java, which includes all the buttons and fields in one Form.

Also, you can use the Java compiler to compile a program.

Following are the constructors that can be used to create registration form:

1-JFrame(): Constructs a new frame that is initially invisible to the user.

2- JFrame(String title): Creates a new, initially invisible Frame to the user with the named given title.

3- JFrame(String title, GraphicsConfiguration (GC)): It will create a JFrame with the specified title and the specified or configured GraphicsConfiguration of a screen device or monitor.

4-JFrame(GraphicsConfiguration (GC): It will create a Frame in the specified or configured GraphicsConfiguration of a screen device with a blank title.

Implementation.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class frameDemo extends JFrame implements ActionListener
{
	Container c;
	//creating labels for username & pass resp
JLabel u=new JLabel("UserName : ");
JLabel p=new JLabel("Password : ");
	JLabel su=new JLabel("Success");
JLabel msg=new JLabel("Invalid username or password");
 	JTextField un=new JTextField();//creating textfield
JPasswordField pa=new JPasswordField();//creating passwordfield

Font f=new Font("Arial",Font.BOLD,18);//font dec 
JButton bt=new JButton("Login");//creating button
frameDemo()
{
c=this.getContentPane();
c.setLayout(null);
c.setBackground(Color.YELLOW);
u.setBounds(100,50,100,40);//set loc & size
p.setBounds(100,140,100,40);
un.setBounds(250,50,200,40);
pa.setBounds(250,140,200,40);
bt.setBounds(280,250,140,50);
bt.addActionListener(this);            //assigning event handler to button
u.setFont(f);
p.setFont(f);
c.add(u);//adding to container
c.add(p);
c.add(un);
c.add(bt);
c.add(su);
c.add(msg);
}
public void actionPerformed(ActionEvent e){
String user=un.getText();
String pswd=pa.getText();
if(e.getSource()==bt)
	      {                                                      //event handling
if(user.equals("Ashish")&&(pswd.equals("ash123")))
{
su.setBounds(250,320,140,40);
su.setFont(f);
}
else
msg.setBounds(250,340,200,40);
msg.setFont(f);
	     }
}//END OF METHOD
}//END OF CLASS
public static void main(String args[])
{
frameDemo frame=new frameDemo();
frame.setVisible(true);
frame.setTitle("Login Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100,100,700,500);
}//END OF METHOD
}//END OF CLASS

Output.

 C:\A> javac login.java
Registration Form in Windows Form Using Swing in Java

Also View: – Write a program to create a registration form using AWT.

Also View- Write a java program to create a window using swing.

Exit mobile version