How to Create a Matte Border in Java

This tutorial will teach you how to create a matte border in Java. A matte border has a matte pattern. This border can create a color matte border which is solid and an icon matte border which is tiled. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of matteBorder.java. 2. Import the following package library:
  1. import javax.swing.*; // used to access ImageIcon and BorderFactory class
  2. import javax.swing.border.*; //used to access the MatteBorder class
  3. import java.awt.*; // used to access the GridLayout class
3. We will initialize variables in our Main, variable frame as JFrame and button1 labeled "Icon Matte Border",button2 labeled "Color Matte Border" as JButton, and icon as ImageIcon. You can put any pictures in the icon then put it inside on your project folder.
  1.         JFrame frame = new JFrame("Matte Border");
  2.     ImageIcon icon = new ImageIcon("2.jpg");
  3.     JButton button1 = new JButton("Icon Matte Border");
  4.     JButton  button2= new JButton("Color Matte Border");
4. Now, we will create a Matte Border using the MatteBorder class. First, we will have a syntax for the iconized matte border.
  1. public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)
Then code for the matte border of the button1. Have its border to matte border using the setBorder method.
  1.     MatteBorder matteBorder1 = BorderFactory.createMatteBorder(30, 23, 35, 28, icon);
  2.     button1.setBorder(matteBorder1);
On the second matte border, we will use again the matte border class but with a color parameter in the constructor.
  1. public MatteBorder(int top, int left, int bottom, int right, Color color)
In your button2, have this code below to have a color matte border.
  1.     MatteBorder matteBorder2 = BorderFactory.createMatteBorder(30, 23, 35, 28, Color.GREEN);
  2.     button2.setBorder(matteBorder2);
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
  1. frame.getContentPane().setLayout(new GridLayout(1,1));
Add all the buttons to the frame using the add method.
  1.    
  2.         frame.getContentPane().add(button1);
  3.         frame.getContentPane().add(button2);
Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1.         frame.setSize(350,300);
  2.         frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access ImageIcon and BorderFactory class
  2. import javax.swing.border.*; //used to access the MatteBorder class
  3. import java.awt.*; // used to access the GridLayout class
  4. public class matteBorder {
  5.   public static void main(String[] args) {
  6.         JFrame frame = new JFrame("Matte Border");
  7.     ImageIcon icon = new ImageIcon("2.jpg");
  8.     JButton button1 = new JButton("Icon Matte Border");
  9.     JButton  button2= new JButton("Color Matte Border");
  10.    
  11.     MatteBorder matteBorder1 = BorderFactory.createMatteBorder(30, 23, 35, 28, icon);
  12.     button1.setBorder(matteBorder1);
  13.    
  14.     MatteBorder matteBorder2 = BorderFactory.createMatteBorder(30, 23, 35, 28, Color.GREEN);
  15.     button2.setBorder(matteBorder2);
  16.  
  17.     frame.getContentPane().setLayout(new GridLayout(1,1));
  18.    
  19.         frame.getContentPane().add(button1);
  20.         frame.getContentPane().add(button2);
  21.         frame.setSize(350,300);
  22.         frame.setVisible(true);
  23.   }
  24. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment