Add Border to JPanel
Java Programming

Add  Border to  JPanel

 

Borders are basically margined around the edges of a Swing component. Borders are useful for providing titles and empty spaces around components. The setBorder() is used to put a border around a component.

Borders in JPanel Example

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

import java.awt.event.*;

public class VariousBorders extends JPanel

{

Border border;

JButton butn;

public VariousBorders()

{

setLayout(new GridLayout(2,3,5,5));

butn=new JButton(“Empty Border”);

//create a button with an empty border

border=new EmptyBorder(1,1,1,1);//top=1,left=1,bottom=1,right=1

butn.setBorder(border);

add(butn);//add button to the panel

//create a button with raised and etched border

butn=new JButton(“Etched and Raised Border”);

border=new EtchedBorder(EtchedBorder.RAISED);//set the border(raised&etched)

butn.setBorder(border);

add(butn);

//create a button with color and etched border

butn=new JButton(“Etched and Color Border”);

butn.setOpaque(true);

border=new BevelBorder(EtchedBorder.RAISED,Color.red,Color.blue);//highlight and shadow

butn.setBorder(border);

add(butn);//add button to the panel

//create a button with a bevel border raised

butn=new JButton(“Bevel Lowered”);

border=new BevelBorder(BevelBorder.LOWERED);//set the border(bevel)

butn.setBorder(border);

add(butn);

//create a button with a bevel border lowered

butn=new JButton(“Etched and Lowered Border”);

Icon icon=new ImageIcon(“aa.gif”);

//create a button with a specified inset

border=new MatteBorder(50,20,50,20,icon);//top,left,bottom,right

butn.setBorder(border);

add(butn);

//create a button with a matte border

butn=new JButton(“Matte Border”);

//highlight and shadow

border=new BevelBorder(EtchedBorder.LOWERED,Color.gray,Color.yellow);

butn.setBorder(border);

add(butn);

}

public static void main(String aa[])

{

//calls the VariousBorders constructor

VariousBorders varBrds=new VariousBorders();

JFrame frame=new JFrame(“Different Borders Layout in Swing”);//set the text on the frame

frame.setContentPane(varBrds);

//pack()is used to compress the pane with respect to the components placed

frame.pack();

//display the frame

frame.setVisible(true);

}

}

Borders in JPanel

Explanation….

The package javax.swing.border contains a list of classes for practical borders.

Empty Border—used to draw the transparent margin without any associative drawing.

Etched Border—border either produces an etched-in or etched-out effect for a component.

Matte Border—The matte pattern is created by either a solid color or a titled icon.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment