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
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
Also View: – Write a program to create a registration form using AWT.
Also View- Write a java program to create a window using swing.
Thanks
Thanks for Posting. Keep up the great effort.|
I feel that is among the most significant information for me. And I am glad studying your article. But should statement on some basic issues, The website style is great, the articles is actually great.
The Fitbit One and the Fitbit Zipp had been the primary wi-fi activity trackers to sync utilizing Bluetooth 4.zero
or Bluetooth SENSIBLE expertise.
I really like looking through your post and I think this website got some really useful stuff on it!
Spot on with this write-up, I actually think this web site needs far more attention. I’ll probably be returning to read more, thanks for the info!