Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
94 views
in JAVA by (176k points)
How do you create a BoxLayout in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (176k points)

To create a BoxLayout in Java, you need to specify the container on which you want to apply the layout and the desired orientation (horizontal or vertical). 

Here's an example:

import javax.swing.*;

public class BoxLayoutExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("BoxLayout Example");
        JPanel panel = new JPanel();
        
        // Set the panel's layout manager to BoxLayout
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        
        // Add components to the panel
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
        panel.add(new JButton("Button 3"));
        
        // Add the panel to the frame
        frame.getContentPane().add(panel);
        
        // Set frame properties and make it visible
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
 

This example creates a JFrame with a JPanel that uses a vertical BoxLayout. It adds three JButton components to the panel.

Related questions

0 votes
1 answer
asked May 16, 2023 in JAVA by kvdevika (176k points)
0 votes
1 answer
asked May 16, 2023 in JAVA by kvdevika (176k points)
0 votes
2 answers
asked May 16, 2023 in JAVA by kvdevika (176k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...