0

I'm trying to understand GUI in java. I started with making a simple login page. Here, I'm stacking a heading panel, body panel and a footer panel, in a box layout (Stacked vertically). All this in another parent panel, which is then put into the frame's content pane (center, border layout). However, the body panel, inside the parent panel is getting all stretched up vertically. Having a very tough time fixing it, any suggestions?

I don't want to use spring layout, etc here.

setSize, setMinimumSize, setMaximumSize don't seem to be working here at all.

Please help

        JButton submit = new JButton("Login");                  //Directly added to the frame
        JLabel head1 = new JLabel("LOGIN SCREEN");              //Component of head panel
        JLabel userName = new JLabel("Username ");              //Component of body panel 1
        JTextField userNameField = new JTextField(15);          //Component of body panel 1
        JLabel userNameExtra = new JLabel(" *");                //Component of body panel 1
        userNameExtra.setForeground(Color.red);
        JLabel pass = new JLabel("Password ");                  //Component of body panel 2
        JPasswordField passField = new JPasswordField(15);              //Component of body panel 2
        JLabel passExtra = new JLabel(" *");                    //Component of body panel 2
        passExtra.setForeground(Color.red);
        JLabel footer = new JLabel ("*This field is mandatory");
        JLabel credits = new JLabel("-by Nikhil");

        //---Preparing the panels with their components---
        headPanel.setLayout(new BoxLayout(headPanel,BoxLayout.Y_AXIS));
        headPanel.add(head1);
        //userNamePanel.setLayout(new BoxLayout(userNamePanel,BoxLayout.X_AXIS));
        userNamePanel.add(userName);
        userName.requestFocus();
        userNamePanel.add(userNameField);
        userNamePanel.add(userNameExtra);
        //passPanel.setLayout(new BoxLayout(passPanel,BoxLayout.X_AXIS));
        passPanel.add(pass);
        passPanel.add(passField);
        passPanel.add(passExtra);
        bodyPanel.setLayout(new BoxLayout(bodyPanel,BoxLayout.Y_AXIS));
        bodyPanel.add(userNamePanel);
        bodyPanel.add(passPanel);
        bodyPanel.add(submit);
        footerPanel.setLayout(new BoxLayout(footerPanel,BoxLayout.Y_AXIS));
        footerPanel.add(footer);

        //---Preparing the parent panel with its components (other panels)---
        parentPanel.setLayout(new BoxLayout(parentPanel,BoxLayout.Y_AXIS));
        parentPanel.add(headPanel);
        parentPanel.add(bodyPanel);
        parentPanel.add(footerPanel);

        //---Putting the parent panel in the frame---
        frame.setTitle("Login GUI Application");
        frame.getContentPane().add(parentPanel,"Center");
        frame.getContentPane().add(credits,"South");
        frame.setSize(500,500);
        frame.setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nikhil
  • 163
  • 2
  • 14
  • 2
    1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 20 '13 at 10:24
  • You may be seeing the effect examined in [BoxLayout ignores setYAlighment](http://stackoverflow.com/questions/18744960/boxlayout-ignores-setyalighment) – trashgod Oct 20 '13 at 15:53

0 Answers0