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

How to Create an Etched Border in Java

tomee321

Growth Optimization Specialist
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
97
Likes
157
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial will teach you how to create an etched border in Java. An etched border has an etched-in or etched-out effect and in default it is in etched-in effect. Like bevel borders, it has also constants of raised and lowered etched.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of etchedBorder.java.

2. Import the following package library:
  1. import

    java.awt.*

    ;

    //used to access the GridLayout and Color class
  2. import

    javax.swing.*

    ;

    //used to access the JFrame and JButton class
  3. import

    javax.swing.border.*

    ;

    //used to access the Border and EtchedBorder class

3. We will initialize variables in our Main, variable frame as JFrame and button1 labeled "Normal Etched",button2 labeled "Raised Etched", and button3 labeled "Lowered Etched" as JButton.

  1. JFrame

    frame =

    new

    JFrame

    (

    "Etched Borders"

    )

    ;
  2. JButton

    button1 =

    new

    JButton

    (

    "Normal Etched"

    )

    ;
  3. JButton

    button2 =

    new

    JButton

    (

    "Raised Etched"

    )

    ;
  4. JButton

    button3 =

    new

    JButton

    (

    "Lowered Etched"

    )

    ;

4. Now, we will create an etched border for button1 and we will use the Border class here with the EtchedBorder class instantiation. We will name the Border as normalBorder.
  1. Border

    normalBorder =

    new

    EtchedBorder

    (

    )

    ;
  2. button1.setBorder

    (

    normalBorder)

    ;

Then we will create another etched border and will named it as raisedBorder because it will have an etched of raised, color red, and color blue. This border will set to the button2.
  1. Border

    raisedBorder =

    new

    EtchedBorder

    (

    EtchedBorder

    .RAISED

    , Color

    .RED

    , Color

    .BLUE

    )

    ;
  2. button2.setBorder

    (

    raisedBorder)

    ;

Lastly, we will create another etched border and will named it as loweredBorder because it will have an etched lowered, color yellow, and color green. This border will set to the button3.
  1. Border

    loweredBorder =

    new

    EtchedBorder

    (

    EtchedBorder

    .LOWERED

    , Color

    .YELLOW

    , Color

    .GREEN

    )

    ;
  2. button3.setBorder

    (

    loweredBorder)

    ;

5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
  1. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    1

    , 2

    )

    )

    ;

Add all the buttons to the frame using the add method.
  1. frame.getContentPane

    (

    )

    .add

    (

    button1 )

    ;
  2. frame.getContentPane

    (

    )

    .add

    (

    button2)

    ;
  3. frame.getContentPane

    (

    )

    .add

    (

    button3)

    ;

Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setSize

    (

    350

    , 200

    )

    ;
  2. frame.setVisible

    (

    true

    )

    ;
  3. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;


Output:

etchedborder.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 and JButton class
  3. import

    javax.swing.border.*

    ;

    //used to access the Border and EtchedBorder class

  4. public

    class

    etchedBorder {
  5. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  6. JFrame

    frame =

    new

    JFrame

    (

    "Etched Borders"

    )

    ;
  7. JButton

    button1 =

    new

    JButton

    (

    "Normal Etched"

    )

    ;
  8. JButton

    button2 =

    new

    JButton

    (

    "Raised Etched"

    )

    ;
  9. JButton

    button3 =

    new

    JButton

    (

    "Lowered Etched"

    )

    ;

  10. Border

    normalBorder =

    new

    EtchedBorder

    (

    )

    ;
  11. button1.setBorder

    (

    normalBorder)

    ;

  12. Border

    raisedBorder =

    new

    EtchedBorder

    (

    EtchedBorder

    .RAISED

    , Color

    .RED

    , Color

    .BLUE

    )

    ;
  13. button2.setBorder

    (

    raisedBorder)

    ;

  14. Border

    loweredBorder =

    new

    EtchedBorder

    (

    EtchedBorder

    .LOWERED

    , Color

    .YELLOW

    , Color

    .GREEN

    )

    ;
  15. button3.setBorder

    (

    loweredBorder)

    ;

  16. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    1

    , 2

    )

    )

    ;
  17. frame.getContentPane

    (

    )

    .add

    (

    button1 )

    ;
  18. frame.getContentPane

    (

    )

    .add

    (

    button2)

    ;
  19. frame.getContentPane

    (

    )

    .add

    (

    button3)

    ;
  20. frame.setSize

    (

    350

    , 200

    )

    ;
  21. frame.setVisible

    (

    true

    )

    ;
  22. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  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


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

452,292

325,536

325,544

Top