|
| 1 | +package layout; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.Color; |
| 5 | +import java.awt.Container; |
| 6 | +import java.awt.Dimension; |
| 7 | +import javax.swing.JFrame; |
| 8 | +import javax.swing.JLabel; |
| 9 | +import javax.swing.SwingConstants; |
| 10 | +import javax.swing.SwingUtilities; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @author rafiul islam |
| 15 | + */ |
| 16 | +public class TheBorderLayout { |
| 17 | + /** |
| 18 | + * Border Layout provide a directional based layout. |
| 19 | + * A component can be set after, before, left , right, |
| 20 | + * top bottom of another. |
| 21 | + * |
| 22 | + * BorderLayout class provide some static positioning string |
| 23 | + * to set the components on a preferred position |
| 24 | + * |
| 25 | + * @see java.awt.BorderLayout |
| 26 | + */ |
| 27 | + |
| 28 | + public TheBorderLayout(){ |
| 29 | + |
| 30 | + JFrame frame = new JFrame("Border Layout");// create a frame window |
| 31 | + frame.setPreferredSize(new Dimension(400, 220));// set dimension instead of position on screen |
| 32 | + frame.setResizable(true);// set resize of the window frame true |
| 33 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set stop the program for close the frame |
| 34 | + |
| 35 | + Container pane = frame.getContentPane();// get the content panel of the frame |
| 36 | + pane.setLayout(new BorderLayout());// set the panel layout |
| 37 | + |
| 38 | + JLabel top = new JLabel("I AM FROM TOP",SwingConstants.CENTER);//create a jlabel and set the text on center |
| 39 | + top.setBackground(Color.red); //set the jabel background color |
| 40 | + top.setOpaque(true);// set background color visible |
| 41 | + |
| 42 | + JLabel bottom = new JLabel("I AM FROM BOTTOM",SwingConstants.CENTER); |
| 43 | + bottom.setBackground(Color.LIGHT_GRAY); |
| 44 | + bottom.setOpaque(true); |
| 45 | + |
| 46 | + JLabel right = new JLabel("I AM FROM RIGHT",SwingConstants.CENTER); |
| 47 | + right.setBackground(Color.ORANGE); |
| 48 | + right.setOpaque(true); |
| 49 | + |
| 50 | + JLabel left = new JLabel("I AM FROM LEFT",SwingConstants.CENTER); |
| 51 | + left.setBackground(Color.GREEN); |
| 52 | + left.setOpaque(true); |
| 53 | + |
| 54 | + JLabel center = new JLabel("I AM FROM CENTER",SwingConstants.CENTER); |
| 55 | + center.setBackground(Color.PINK); |
| 56 | + center.setOpaque(true); |
| 57 | + |
| 58 | + pane.add(top, BorderLayout.PAGE_START);// set on the top |
| 59 | + pane.add(left, BorderLayout.WEST); // set on the left |
| 60 | + pane.add(right, BorderLayout.EAST); // set on the right |
| 61 | + pane.add(bottom, BorderLayout.PAGE_END); // set on the bottom |
| 62 | + pane.add(center, BorderLayout.CENTER); // set on the center |
| 63 | + |
| 64 | + frame.pack(); // pack everything on the frame and ready to display |
| 65 | + frame.setVisible(true); // set frame visible |
| 66 | + } |
| 67 | + |
| 68 | + public static class WithRowCol{ |
| 69 | + public WithRowCol(){ |
| 70 | + JFrame frame = new JFrame("Border Layout With GAP");//create a frame window |
| 71 | + frame.setPreferredSize(new Dimension(400, 220)); //set dimension |
| 72 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set close to exit |
| 73 | + frame.setResizable(true); // set frame resizable |
| 74 | + |
| 75 | + Container pane = frame.getContentPane(); // get content panel from frame |
| 76 | + pane.setLayout(new BorderLayout(20, 10));// set layout with Horizontal and Vertical Px gap |
| 77 | + |
| 78 | + JLabel top = new JLabel("I AM FROM TOP",SwingConstants.CENTER);//create a jlabel and set the text on center |
| 79 | + top.setBackground(Color.red); //set the jabel background color |
| 80 | + top.setOpaque(true);// set background color visible |
| 81 | + |
| 82 | + JLabel bottom = new JLabel("I AM FROM BOTTOM",SwingConstants.CENTER); |
| 83 | + bottom.setBackground(Color.LIGHT_GRAY); |
| 84 | + bottom.setOpaque(true); |
| 85 | + |
| 86 | + JLabel right = new JLabel("I AM FROM RIGHT",SwingConstants.CENTER); |
| 87 | + right.setBackground(Color.ORANGE); |
| 88 | + right.setOpaque(true); |
| 89 | + |
| 90 | + JLabel left = new JLabel("I AM FROM LEFT",SwingConstants.CENTER); |
| 91 | + left.setBackground(Color.GREEN); |
| 92 | + left.setOpaque(true); |
| 93 | + |
| 94 | + JLabel center = new JLabel("I AM FROM CENTER",SwingConstants.CENTER); |
| 95 | + center.setBackground(Color.PINK); |
| 96 | + center.setOpaque(true); |
| 97 | + |
| 98 | + pane.add(top, BorderLayout.PAGE_START);// set on the top |
| 99 | + pane.add(left, BorderLayout.WEST); // set on the left |
| 100 | + pane.add(right, BorderLayout.EAST); // set on the right |
| 101 | + pane.add(bottom, BorderLayout.PAGE_END); // set on the bottom |
| 102 | + pane.add(center, BorderLayout.CENTER); // set on the center |
| 103 | + frame.pack(); |
| 104 | + frame.setVisible(true); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // driver function |
| 109 | + public static void main(String[] args) { |
| 110 | + //create a swing thread for run first class |
| 111 | + SwingUtilities.invokeLater(new Runnable(){ |
| 112 | + public void run(){ |
| 113 | + new TheBorderLayout(); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + //create another swing thread for run the sub-class |
| 118 | + SwingUtilities.invokeLater(new Runnable(){ |
| 119 | + public void run(){ |
| 120 | + new TheBorderLayout.WithRowCol(); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments