top of page
minalpatel1597

Java GUI Toolkit


Overview of GUI- Graphical User Interface

What is Swing in Java?


Swing is GUI toolkits that include Java Swing Components. These components are basic building blocks of Java Swing Application. In this chapter, we will use JFrame, JButton, JLabel components.

Let's write a code to create a Frame!!

Frame: It is fully functioning window with its title and icon.


package practice. Code;

import java.awt.TextField;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class gui {
	
	public gui(){
	//implement the constructor 
		
		JFrame frame = new JFrame();
		frame.setTitle("This is my Frame"); 
		frame.setSize(400,400);
                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO);
		frame.setVisible(true);
}

Output:

When you run the above code, it will open another window, That is your "Frame".



Now Time for Panel!!

Panel: It is container within the window(Frame), The purpose pf Panel is to organize the components on to the window.

package practice.code;

import java.awt.TextField;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class gui {
	
	public gui(){//implement the constructor 
		
		JFrame frame = new JFrame();
		
		frame.add(panel);
		frame.setTitle("This is my Frame");
		frame.setSize(400,400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		JPanel panel = new JPanel();
		frame.add(panel);   //Adding a Log in Panel to the Frame
		
		
		

Customizing appearance of Panel:

Set background color:

panel.setBackground(Color. GRAY);

Set Transparent background:

panel.setOpaque(false);

Set different border around the panel:

panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));		panel.setBorder(BorderFactory.createEtchedBorder());

Make panel visible:

panel.setVisible(true);

Add labels and Text fields (Text Box) in the panel:

JLabel Userlabel = new JLabel("User"); //Username label
TextField userText = new TextField(20);//creates a box to enter user name
userText.setBounds(100, 20, 165, 25);//set boundary for the text box
		
JLabel PasswordLabel = new JLabel("Password"); //Password label 
JPasswordField passwordText = new JPasswordField();//creates a box to enter password		
passwordText.setBounds(100, 50, 165, 25); //set boundary for the text box

Output:











Finally, Last component to finish log in !!

JButton Button = new JButton("Log in");

Setting boundaries on the button:

Button.setBounds(10, 80, 80, 25);

Adding the button to the panel:

panel.add(Button);

This is how your final code should look like at the end:


package practice.code;

import java.awt.Color;
import java.awt.TextField;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.BevelBorder;

public class gui {
	
	public gui(){//implement the constructor 
		
		JFrame frame = new JFrame();
		
		JPanel panel = new JPanel();
		
		JButton Button = new JButton("Log in");
		
		JLabel label = new JLabel("User");
		
		TextField userText = new TextField(20);
		userText.setBounds(100, 20, 165, 25);
		JLabel PasswordLabel = new JLabel("Password");
		PasswordLabel.setBounds(10, 50, 80, 25);
		
		JPasswordField passwordText = new JPasswordField();
		passwordText.setBounds(100, 50, 165, 25);
		
		Button.setBounds(10, 80, 80, 25);
		panel.add(PasswordLabel);
		panel.add(passwordText);
		
		label.setBounds(10, 20, 80, 25);
		panel.add(label);
        panel.add(userText);
		panel.add(Button);
		
		panel.setBackground(Color.GRAY);
		panel.setOpaque(false);
		panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		panel.setBorder(BorderFactory.createEtchedBorder());
		panel.setLayout(null);
		panel.setVisible(true);
		      
		frame.add(panel);
		frame.setTitle("This is my Frame");
		frame.setSize(400,400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);	
		
	}

Output:


















Was the blog helpful?

  • Yes

  • No


50 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page