CI 2019 von Daniel, Eugen und Michael
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.4 KiB

6 years ago
  1. Java AWT | BorderLayout Class
  2. BorderLayout is the default layout for the window objects such as JFrame, JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the components in the five regions. Four sides are referred to as north, south, east, and west. The middle part is called the center. Each region can contain only one component and is identified by a corresponding constant as NORTH, SOUTH, EAST, WEST, and CENTER.
  3. Constructors:
  4. BorderLayout(): It will construct a new borderlayout with no gaps between the components.
  5. BorderLayout(int, int): It will constructs a border layout with the specified gaps between the components.
  6. Commonly Used Methods:
  7. toString(): Returns a string which is representation of the state of border layout.
  8. getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-axis.
  9. getLayoutAlignmentY(Container parent): It will returns the layout alignment along the Y-axis.
  10. removeLayoutComponent(Component comp): This method is used to remove the specified component from the borderlayout.
  11. getVgap(): Return the vertical gap between the components.
  12. getHgap(): Returns the Horizontal gap between the components.
  13. setHgap(int hgap): It is used to set the horizontal gap between the components.
  14. setVgap(int vgap): It is used to set the vertical gap between the components.
  15. Below Programs will illustrate the BorderLayout class:
  16. Program 1: The following program creates JButton components in a JFrame, whose instance class is “BorderLayoutDemo”. We create 5 JButton and then add them to the JFrame by using add() method. We will set the size and visibility of the frame by using setSize() and setVisible() method respectively. The layout is set by using the setLayout() method.
  17. filter_none
  18. brightness_4
  19. // Java program to illustrate the BorderLayout
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import javax.swing.*;
  23. // class extends JFrame
  24. class BoderLayoutDemo extends JFrame {
  25. BoderLayoutDemo()
  26. {
  27. // Creating Object of Jpanel class
  28. JPanel pa = new JPanel();
  29. // set the layout
  30. pa.setLayout(new BorderLayout());
  31. // add a new JButton with name "wel" and it is
  32. // lie top of the container
  33. pa.add(new JButton("WelCome"), BorderLayout.NORTH);
  34. // add a new JButton with name "come" and it is
  35. // lie buttom of the container
  36. pa.add(new JButton("Geeks"), BorderLayout.SOUTH);
  37. // add a new JButton with name "Layout" and it is
  38. // lie left of the container
  39. pa.add(new JButton("Layout"), BorderLayout.EAST);
  40. // add a new JButton with name "Border" and it is
  41. // lie right of the container
  42. pa.add(new JButton("Border"), BorderLayout.WEST);
  43. // add a new JButton with name "hello everybody" and it is
  44. // lie center of the container
  45. pa.add(new JButton("GeeksforGeeks"), BorderLayout.CENTER);
  46. // add the pa object which refer to the Jpanel
  47. add(pa);
  48. // Function to close the operation of JFrame.
  49. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. // Function to set size of JFrame.
  51. setSize(300, 300);
  52. // Function to set visible status of JFrame.
  53. setVisible(true);
  54. }
  55. }
  56. class MainFrame {
  57. // Driver code
  58. public static void main(String[] args)
  59. {
  60. // calling the constructor
  61. new BoderLayoutDemo();
  62. }
  63. }