How to Create a Line Border in Java

This tutorial will teach you how to create a line border in java. A line border has only one line with a single color and has its own thickness for its border. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of lineBorders.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the GridLayout and Color class
  2. import javax.swing.*; //used to access the JFrame, JButton, and BorderFactory class
  3. import javax.swing.border.*; //used to access the Border and LineBorder subclass of the border class in swing
3. We will initialize variables in our Main, variable frame as JFrame and button1 labeled "1 pixel",button2 labeled "10 pixel", and button3 labeled "Round 5 pixel" as JButton.
  1. JFrame frame = new JFrame("Line Borders");
  2. JButton button1 = new JButton("1 Pixel");
  3. JButton button2 = new JButton("10 Pixel");
  4. JButton button3 = new JButton("Rounded 5 Pixel");
4. Now, we will create a 1 pixel line border for Button1. We will use the BorderFactory class of the Border class. We will color the line as a yellow color. We will also use the setBorder method of the button to have the border.
  1. Border thinBorder = BorderFactory.createLineBorder(Color.YELLOW);
  2. button1.setBorder(thinBorder);
For the thicker border, we will add another parameter to the color which is the thickness of the line in a pixel value. We will put the 10 pixel line border for button2.
  1. Border thickBorder = new LineBorder(Color.RED, 10);
  2. button2.setBorder(thickBorder);
For a round line border, aside from putting color of the line and its thickness, we will have Boolean value for the rounded line. Which means if we set it to True then it will be rounded. Otherwise, it is not. We will set this border in button3.
  1. Border roundedBorder = new LineBorder(Color.GREEN, 5, true);
  2. button3.setBorder(roundedBorder);
Add all the buttons in the frame using the add method.
  1. frame.getContentPane().add(button1);
  2. frame.getContentPane().add(button2);
  3. frame.getContentPane().add(button3);
5. Have your Layout in Grid. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane().setLayout(new GridLayout(1,2));
  2.  
  3. frame.pack();
  4. frame.setSize(300, 100);
  5. frame.setVisible(true);
  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the GridLayout and Color class
  2. import javax.swing.*; //used to access the JFrame, JButton, and BorderFactory class
  3. import javax.swing.border.*; //used to access the Border and LineBorder subclass of the border class in swing
  4.  
  5.  
  6. public class lineBorders {
  7. public static void main(String args[]) {
  8. JFrame frame = new JFrame("Line Borders");
  9. JButton button1 = new JButton("1 Pixel");
  10. JButton button2 = new JButton("10 Pixel");
  11. JButton button3 = new JButton("Rounded 5 Pixel");
  12.  
  13.  
  14. Border thinBorder = BorderFactory.createLineBorder(Color.YELLOW);
  15. button1.setBorder(thinBorder);
  16.  
  17.  
  18. Border thickBorder = new LineBorder(Color.RED, 10);
  19. button2.setBorder(thickBorder);
  20.  
  21.  
  22. Border roundedBorder = new LineBorder(Color.GREEN, 5, true);
  23. button3.setBorder(roundedBorder);
  24.  
  25. frame.getContentPane().add(button1);
  26. frame.getContentPane().add(button2);
  27. frame.getContentPane().add(button3);
  28.  
  29. frame.getContentPane().setLayout(new GridLayout(1,2));
  30.  
  31. frame.pack();
  32. frame.setSize(300, 100);
  33. frame.setVisible(true);
  34. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. }
  36. }
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