1

So I'm trying to make a GUI for a simple program, but the panel with the FlowLayout keeps adding random white space and messing everything up! How can I fix this?

Basically, what I want is this:

enter image description here

But when I try, I get this... :

enter image description here

Code:

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class MainMenu extends JFrame {

    String username = "undefined";
    String program = "undefined";
    String year = "undefined";
    String term = "undefined";

    static DefaultListModel<String> coursesModel;
    static JList<String> coursesList;

    public MainMenu() {

        JFrame mainMenuF = new JFrame("Main menu");

        JButton editPersonalInfoB = new JButton("Edit personal information");
        editPersonalInfoB.setContentAreaFilled(false);
        editPersonalInfoB.setFocusPainted(false);
        editPersonalInfoB.setOpaque(false);
        editPersonalInfoB.setBorder(null);
        editPersonalInfoB.setBorderPainted(false);
        editPersonalInfoB.setMargin(new Insets(0,0,0,0));
        editPersonalInfoB.setCursor(new Cursor(Cursor.HAND_CURSOR));
        editPersonalInfoB.setAlignmentX(Component.LEFT_ALIGNMENT);
        JButton addNewCourseB = new JButton("Add new course");
        addNewCourseB.setAlignmentX(Component.LEFT_ALIGNMENT);
        JButton viewTermGPAB = new JButton("View term GPA");
        viewTermGPAB.setAlignmentX(Component.LEFT_ALIGNMENT);
        JButton removeCourseB = new JButton("Remove");
        removeCourseB.setAlignmentY(Component.RIGHT_ALIGNMENT);
        JButton editCourseB = new JButton("Edit");
        editCourseB.setAlignmentY(Component.RIGHT_ALIGNMENT);
        JButton viewCourseB = new JButton("View");
        viewCourseB.setAlignmentY(Component.RIGHT_ALIGNMENT);

        JLabel welcomeL = new JLabel("Welcome, " + username + "!");
        welcomeL.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel programAndYearL = new JLabel("Program: " + program + "   " + "Year: " + year);
        programAndYearL.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel termL = new JLabel("Term: " + term);
        termL.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel space1 = new JLabel("  ");
        JLabel space2 = new JLabel("  ");

        coursesModel = new DefaultListModel<String>();
        coursesList = new JList<String>(coursesModel);
        JScrollPane coursesScroller = new JScrollPane(coursesList);
        coursesScroller.setPreferredSize(new Dimension(100, 100));
        coursesScroller.setBorder(BorderFactory.createTitledBorder("Your courses"));
        coursesScroller.setViewportView(coursesList);
        coursesScroller.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel top = new JPanel();
        top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
        top.add(welcomeL);
        top.add(programAndYearL);
        top.add(termL);
        top.add(editPersonalInfoB);

        JPanel bigButtons = new JPanel();
        bigButtons.setLayout(new BoxLayout(bigButtons, BoxLayout.PAGE_AXIS));
        bigButtons.add(addNewCourseB);
        bigButtons.add(viewTermGPAB);

        JPanel smallButtons = new JPanel();
        smallButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
        smallButtons.add(removeCourseB);
        smallButtons.add(editCourseB);
        smallButtons.add(viewCourseB);

        JPanel all = new JPanel();
        all.setLayout(new BoxLayout(all, BoxLayout.PAGE_AXIS));
        all.add(top);
        all.add(space1);
        all.add(coursesScroller);
        all.add(space2);
        all.add(smallButtons);
        all.add(bigButtons);
        all.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        mainMenuF.add(all);
        mainMenuF.setVisible(true);
        mainMenuF.setSize(730, 490);
        mainMenuF.setLocation(50,50);
//      mainMenuF.setResizable(false);
        mainMenuF.setDefaultCloseOperation(EXIT_ON_CLOSE);          
    }

    public static void main(String[] args) {
        MainMenu main = new MainMenu();
    }
}

Thanks in advance!

lrogar
  • 59
  • 1
  • 8
  • 1
    `mainMenuF.setVisible(true); mainMenuF.setSize(730, 490); mainMenuF.setLocation(50,50);` should probably be `mainMenuF.pack(); mainMenuF.setLocationByPlatform(true); mainMenuF.setVisible(true);` .. – Andrew Thompson Apr 20 '15 at 22:55
  • 1
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 20 '15 at 23:01
  • Try using a `GridBagLayout` instead of a `BoxLayout`, see [How to Use GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for more details – MadProgrammer Apr 21 '15 at 00:11

0 Answers0