• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Line Border in Java

walteragortey

Blockchain Visionary
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
48
Likes
156
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. frame.pack

    (

    )

    ;
  3. frame.setSize

    (

    300

    , 100

    )

    ;
  4. frame.setVisible

    (

    true

    )

    ;
  5. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;


Output:

lineborder.png


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. public

    class

    lineBorders {
  5. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  6. JFrame

    frame =

    new

    JFrame

    (

    "Line Borders"

    )

    ;
  7. JButton

    button1 =

    new

    JButton

    (

    "1 Pixel"

    )

    ;
  8. JButton

    button2 =

    new

    JButton

    (

    "10 Pixel"

    )

    ;
  9. JButton

    button3 =

    new

    JButton

    (

    "Rounded 5 Pixel"

    )

    ;


  10. Border

    thinBorder =

    BorderFactory

    .createLineBorder

    (

    Color

    .YELLOW

    )

    ;
  11. button1.setBorder

    (

    thinBorder)

    ;


  12. Border

    thickBorder =

    new

    LineBorder

    (

    Color

    .RED

    , 10

    )

    ;
  13. button2.setBorder

    (

    thickBorder)

    ;


  14. Border

    roundedBorder =

    new

    LineBorder

    (

    Color

    .GREEN

    , 5

    , true

    )

    ;
  15. button3.setBorder

    (

    roundedBorder)

    ;

  16. frame.getContentPane

    (

    )

    .add

    (

    button1)

    ;
  17. frame.getContentPane

    (

    )

    .add

    (

    button2)

    ;
  18. frame.getContentPane

    (

    )

    .add

    (

    button3)

    ;

  19. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    1

    ,2

    )

    )

    ;

  20. frame.pack

    (

    )

    ;
  21. frame.setSize

    (

    300

    , 100

    )

    ;
  22. frame.setVisible

    (

    true

    )

    ;
  23. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  24. }
  25. }

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top