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.

88 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
  1. Java AWT | BorderLayout Class
  2. https://www.geeksforgeeks.org/java-awt-borderlayout-class/
  3. 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.
  4. Constructors:
  5. BorderLayout(): It will construct a new borderlayout with no gaps between the components.
  6. BorderLayout(int, int): It will constructs a border layout with the specified gaps between the components.
  7. Commonly Used Methods:
  8. toString(): Returns a string which is representation of the state of border layout.
  9. getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-axis.
  10. getLayoutAlignmentY(Container parent): It will returns the layout alignment along the Y-axis.
  11. removeLayoutComponent(Component comp): This method is used to remove the specified component from the borderlayout.
  12. getVgap(): Return the vertical gap between the components.
  13. getHgap(): Returns the Horizontal gap between the components.
  14. setHgap(int hgap): It is used to set the horizontal gap between the components.
  15. setVgap(int vgap): It is used to set the vertical gap between the components.
  16. Below Programs will illustrate the BorderLayout class:
  17. 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.
  18. filter_none
  19. brightness_4
  20. // Java program to illustrate the BorderLayout
  21. import java.awt.*;
  22. import java.awt.event.*;
  23. import javax.swing.*;
  24. // class extends JFrame
  25. class BoderLayoutDemo extends JFrame {
  26. BoderLayoutDemo()
  27. {
  28. // Creating Object of Jpanel class
  29. JPanel pa = new JPanel();
  30. // set the layout
  31. pa.setLayout(new BorderLayout());
  32. // add a new JButton with name "wel" and it is
  33. // lie top of the container
  34. pa.add(new JButton("WelCome"), BorderLayout.NORTH);
  35. // add a new JButton with name "come" and it is
  36. // lie buttom of the container
  37. pa.add(new JButton("Geeks"), BorderLayout.SOUTH);
  38. // add a new JButton with name "Layout" and it is
  39. // lie left of the container
  40. pa.add(new JButton("Layout"), BorderLayout.EAST);
  41. // add a new JButton with name "Border" and it is
  42. // lie right of the container
  43. pa.add(new JButton("Border"), BorderLayout.WEST);
  44. // add a new JButton with name "hello everybody" and it is
  45. // lie center of the container
  46. pa.add(new JButton("GeeksforGeeks"), BorderLayout.CENTER);
  47. // add the pa object which refer to the Jpanel
  48. add(pa);
  49. // Function to close the operation of JFrame.
  50. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. // Function to set size of JFrame.
  52. setSize(300, 300);
  53. // Function to set visible status of JFrame.
  54. setVisible(true);
  55. }
  56. }
  57. class MainFrame {
  58. // Driver code
  59. public static void main(String[] args)
  60. {
  61. // calling the constructor
  62. new BoderLayoutDemo();
  63. }
  64. }