Pages - GamesandApps

How To Create Borders In Java

Wan to to create BORDERS in Java?

You can add various kinds of borders to your Frame and even your objects used in Java Frames.
Here's a simple demonstration on how to use various borders on your Frame.


If you want to add any single of these Borders to your Frame, it should be simple. If you have done some Java programming you should be able to edit the code and take out an individual Border Style. If you can't, no problem, just ask me, i'll do it for you!

Here's the code, compile as BorderTest.java



import java.awt.*;
import javax.swing.*;
import java.awt.event.* ;
import javax.swing.border.* ;


public class BorderTest extends JFrame //implements WindowListener
{
BorderTest()
{
   JPanel p = new JPanel();
   Border[] border = new Border[] 


       BorderFactory.createEtchedBorder(),
         BorderFactory.createTitledBorder("Border types"),
       BorderFactory.createLoweredBevelBorder(),
       BorderFactory.createRaisedBevelBorder(),
       BorderFactory.createEtchedBorder(),
       BorderFactory.createLineBorder(Color.blue),
       BorderFactory.createMatteBorder(10, 10, 10, 10, Color.blue),
        BorderFactory.createEmptyBorder()
};


    p.setLayout(new GridLayout(border.length, 0, 3, 3));


   for (int i = 0; i < border.length; i++) 
{
     JPanel borderPanel = new JPanel();
     borderPanel.setBorder(border[i]);
     p.add(borderPanel);
}
   getContentPane().add(p, "Center");
   setTitle("BorderTest");
   setSize(600, 400);


addWindowListener(new WindowAdapter() 
{
     public void windowClosing(WindowEvent e) 
{
       System.exit(0);
      }
    });
  }


 public static void main(String[] args) 
 {
   JFrame frame = new BorderTest();
     frame.show();


 }


}


Here's a sample output of the program :-
how to create borders in java - output
Output of Border Test Program

No comments:

Post a Comment