Create empty border with BorderFactory class in Java?

Java 8Object Oriented ProgrammingProgramming

To create empty border, use the createEmptyBorder() method. Let us first create a label component −

JLabel label = new JLabel("Label with empty border!");

Now, create empty border with BorderFactory class −

label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

The following is an example to create empty border −

Example

package my;
import javax.swing.BorderFactory;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingDemo {
   public static void main(String[] args) throws Exception {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("Label with empty border!");
      label.setFont(new Font("Verdana", Font.PLAIN, 16));
      label.setVerticalAlignment(JLabel.BOTTOM);
      label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
      frame.add(label);
      frame.setSize(550,350);
      frame.setVisible(true);
   }
}

Output

raja
Published on 31-May-2019 12:29:33
Advertisements