MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create a form and simple frame with Java Swing

In this article you will see two methods to make a simple frame with Java Swing (using extends or not) and will also see how to create a form using Swing.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Swing is the best option for making desktop application with Java.It provides too many components which are needed for making any desktop app.

Swing is better than the awt because its component is lighter and it has much more support than awt.

This article teaches you several swing components along with their uses in different context

Here we are going to learn following

  1. Making a simple swing frame
  2. Making a simple form

First we will learn how we can make a simple swing frame

Listing 1: Making simple swing frame

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingBasic 
{
	public void start()
	{
		JFrame f=new JFrame("Type the name of frame");
		JLabel l=new JLabel("Anurag jain(csanuragjain)");
		f.add(l);
		f.setSize(400,400);
		f.setVisible(true);
	}
	public static void main(String args[])
	{
		new SwingBasic().start();
	}
}

Here,

  1. We make a class named SwingBasic
  2. We define a method named start
  3. We make a new frame making use of JFrame object. In parameter just pass the name which you want to give to the frame
  4. We make a new Label using JLabel. We wrote the text for this label as an argument. We may also use the setText method.
  5. Now we will need to add the label to the frame so that the label get on the frame.
  6. We set the size of frame using the setSize method. We pass as argument the width and height of frame
  7. We use the setVisible method to make the frame visible

The same frame can be made much more easily as listed here

Listing 2: Making simple swing frame using extends

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SwingBasic extends JFrame
{
	public void start()
	{
		JLabel l=new JLabel();
		l.setText("Anurag jain(csanuragjain)");
		add(l);
		setSize(400,400);
		setVisible(true);
	}
	public static void main(String args[])
	{
		new SwingBasic().start();
	}
}

Here,

  1. We used the extends clause to extend the class JFrame so that now class has all functionality of JFrame.
  2. We make a label but this time we have not added text at time of label object creation.
  3. We add text to label making use of setText method
  4. Since we have extended the frame class so there is no need to create a separate object of JFrame.We may directly use its method

Now we will learn to make a simple form using swing

Listing 3: Making simple swing form

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SwingBasic extends JFrame
{
	JLabel l;
	public void start()
	{
		l=new JLabel();
		l.setText("	Name :");
		JTextField txt=new JTextField();
		txt.setText("Anurag jain(csanuragjain)");
		JPasswordField ps=new JPasswordField(10);
		JButton but=new JButton();
		but.setText("Submit");
		add(l);
		add(txt);
		add(ps);
		add(but);
		but.addActionListener(
				new ActionListener(){
					public void actionPerformed(
							ActionEvent e){
											l.setText("Button clicked");
										  }
									}
							);
		setLayout(new FlowLayout());
		setSize(400,400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void main(String args[])
	{
		new SwingBasic().start();
	}
}

Here,

  1. We have added a simple form with a label, textbox and a button. When the button is clicked the text on label is converted.
  2. We made a class named SwingBasic which extends Jframe
  3. We define a variable l of type JLabel. We made this declaration as top level since we want to use it in multiple classes
  4. Now we make method start.
  5. We make a label using the JLabel and set its text using setText method
  6. We make a textbox using the JTextField and set its text using setText method
  7. We add a password field using the JPasswordField
  8. We make a button using the JButton and set its text using setText method
  9. Now to perform some action on button click we add addActionListener and tell program what to do in case the button gets clicked. Here we change the label text.
  10. We set layout as flowLayout so that each component displays one after another.There are other layouts also and their usage totally depends on your frame layout.
  11. We set default close operation as exit on close so that the application exit in case it is closed.
  12. We define a main method where we call the start method by making an object of this class
  13. We can obtain the text of any component using the getText method

This is all for this article. Meet you next time with some exciting article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login