BoxLayout as Layout Manager in Java

This tutorial is about the BoxLayout as Layout Manager in Java. A BoxLayout as a layout come from the Box class where it provides each component's x and y alignment properties with its sizes. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of boxLayout.java. 2. Import the javax.swing.* package library that is used to access the BoxLayout, JButton, and JFrame class.
  1. import javax.swing.*; // used to access the BoxLayout, JButton, and JFrame class
3. Initialize your variable in your Main, variable frame for JFrame, and 3 buttons namely b1,b2, and b3 as JButton.
  1. JFrame frame = new JFrame("BoxLayout Component");
  2. JButton b1 = new JButton("Button 1");
  3. JButton b2 = new JButton("Button 2");
  4. JButton b3 = new JButton("Button 3");
Now, we will initialize a BoxLayout class and will name it as variable boxLayout. The BoxLayout automatically arranges your components in row or column position, or the x and y axis position alignment properties together with its size. The BoxLayout has one constructor namely the Target Components and the Axis Alignment Property. The Target argument is where the components are to be placed and the Axis argument specifies what alignment that the components must have. There are four axis constants: BoxLayout.X_AXIS, BoxLayout.Y_AXIS, BoxLayout.LINE_AXIS, and BoxLayout.PAGE_AXIS. Now to declare a BoxLayout, have the code below:
  1. BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS);
4. Then add the buttons into the frame using the add method.
  1. frame.getContentPane().add(b1);
  2. frame.getContentPane().add(b2);
  3. frame.getContentPane().add(b3);
To have a BoxLayout as the layout manager, we will use the setLayout method of the frame.
  1. frame.getContentPane().setLayout(boxLayout);
5. Lastly, pack the frame, set visibility, and the close operation of the frame. Have this code below:
  1. frame.pack();
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access the BoxLayout, JButton, and JFrame class
  2. import javax.swing.JButton;
  3. import javax.swing.JFrame;
  4.  
  5. public class boxLayout {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. JFrame frame = new JFrame("BoxLayout Component");
  10. JButton b1 = new JButton("Button 1");
  11. JButton b2 = new JButton("Button 2");
  12. JButton b3 = new JButton("Button 3");
  13. BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS);
  14.  
  15. frame.getContentPane().setLayout(boxLayout);
  16.  
  17. frame.getContentPane().add(b1);
  18. frame.getContentPane().add(b2);
  19. frame.getContentPane().add(b3);
  20.  
  21. frame.pack();
  22. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. frame.setVisible(true);
  24. }
  25. }
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

Add new comment