Search code examples
javaswingjframelayout-managerboxlayout

How do I set box layout for a nameless JFrame?


Ok I am trying to setLayout as BoxLayout for a nameless jFrame, what do I put in place instead of 'WhatDoIPutHere?'

import javax.swing.BoxLayout;
import javax.swing.JFrame;

public class InterestCalculator extends JFrame{

    private static final long serialVersionUID = 1L;

    public static void main(String [] args) {
        InterestCalculator comp = new InterestCalculator();
        comp.FrameHandler();
    }

    public void FrameHandler() {

        setTitle("Template");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BoxLayout(WhatDoIPutHere?, BoxLayout.Y_AXIS));
        pack();
        setVisible(true);
    }
}

Solution

    • Create a JPanel
    • Set the box layout to the panel
    • Add the panel to the frame.

    As an aside, don't extend JFrame unless changing existing functionality or adding new functionality.