FlowLayout(Java Swing)正在运行

FlowLayout(Java Swing)正在运行,java,swing,user-interface,layout-manager,flowlayout,Java,Swing,User Interface,Layout Manager,Flowlayout,因此,我试图为一个简单的程序制作一个GUI,但是带有FlowLayout的面板不断添加随机空白,并把一切都搞糟了!我怎样才能解决这个问题 基本上,我想要的是: 但当我尝试时,我得到了这个…: 代码: 导入java.awt.Component; 导入java.awt.Cursor; 导入java.awt.Dimension; 导入java.awt.FlowLayout; 导入java.awt.Insets; 导入java.awt.Toolkit; 导入javax.swing.*; 导入java

因此,我试图为一个简单的程序制作一个GUI,但是带有FlowLayout的面板不断添加随机空白,并把一切都搞糟了!我怎样才能解决这个问题

基本上,我想要的是:

但当我尝试时,我得到了这个…:

代码:

导入java.awt.Component;
导入java.awt.Cursor;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.Insets;
导入java.awt.Toolkit;
导入javax.swing.*;
导入javax.swing.border.TitledBorder;
公共类主菜单扩展JFrame{
字符串username=“未定义”;
字符串程序=“未定义”;
字符串year=“未定义”;
字符串term=“未定义”;
静态默认列表模型coursemodel;
静态JList课程列表;
公共主菜单(){
JFrame mainMenuF=新JFrame(“主菜单”);
JButton editPersonalInfoB=新JButton(“编辑个人信息”);
editPersonalInfoB.setContentAreaFilled(false);
editPersonalInfoB.setFocusPainted(假);
editPersonalInfoB.set不透明(false);
editPersonalInfoB.setOrder(空);
editPersonalInfoB.setOrderPaint(假);
editPersonalInfoB.setMargin(新插图(0,0,0,0));
editPersonalInfoB.setCursor(新光标(Cursor.HAND_光标));
editPersonalInfoB.setAlignmentX(组件左对齐);
JButton addNewCourseB=新JButton(“添加新课程”);
addNewCourseB.setAlignmentX(组件左对齐);
JButton viewTermGPAB=新JButton(“查看术语GPA”);
viewTermGPAB.setAlignmentX(组件左对齐);
JButton removeCourseB=新JButton(“删除”);
移除courseb.setAlignmentY(组件右对齐);
JButton editCourseB=新JButton(“编辑”);
editCourseB.setAlignmentY(组件右对齐);
JButton viewCourseB=新JButton(“视图”);
viewCourseB.setAlignmentY(组件右对齐);
JLabel welcomeL=新的JLabel(“欢迎,+username+”!”;
welcomeL.setAlignmentX(组件左对齐);
JLabel programAndYearL=新的JLabel(“程序:“+程序+”+“+”年:“+年”);
ProgramandYear.setAlignmentX(组件左对齐);
JLabel termL=新JLabel(“术语:”+术语);
termL.setAlignmentX(组件左对齐);
JLabel space1=新的JLabel(“”);
JLabel space2=新的JLabel(“”);
coursesModel=新的DefaultListModel();
coursesList=新的JList(coursesModel);
JScrollPane coursesScroller=新的JScrollPane(coursesList);
CourseSCroller.setPreferredSize(新尺寸(100100));
coursesScroller.setboorder(BorderFactory.createTitledBorder(“您的课程”);
CourseScroller.setViewportView(coursesList);
CourseSCroller.setAlignmentX(组件左对齐);
JPanel top=新的JPanel();
top.setLayout(新的BoxLayout(top,BoxLayout.Y_轴));
顶部。添加(welcomeL);
顶部。添加(计划和年度);
顶部。添加(termL);
添加(editPersonalInfoB);
JPanel bigButtons=新的JPanel();
bigButtons.setLayout(新的BoxLayout(bigButtons,BoxLayout.PAGE_轴));
添加(addNewCourseB);
添加(viewTermGPAB);
JPanel smallButtons=新的JPanel();
setLayout(新的FlowLayout(FlowLayout.RIGHT));
smallButtons.add(removeCourseB);
smallButtons.add(editCourseB);
smallButtons.add(viewCourseB);
JPanel all=新的JPanel();
all.setLayout(新的BoxLayout(all,BoxLayout.PAGE_轴));
全部。添加(顶部);
全部。添加(空格1);
全部。添加(CourseScroller);
全部。添加(空格2);
添加(小按钮);
添加(大按钮);
all.setboorder(BorderFactory.createEmptyBorder(20,20,20,20));
主菜单添加(全部);
mainMenuF.setVisible(true);
主菜单设置大小(730490);
主菜单设置位置(50,50);
//mainMenuF.SetResizeable(假);
mainMenuF.setDefaultCloseOperation(关闭时退出);
}
公共静态void main(字符串[]args){
MainMenu main=新建MainMenu();
}
}

提前谢谢

mainMenuF.setVisible(true);主菜单设置大小(730490);主菜单设置位置(50,50)
可能应该是
mainMenuF.pack();mainMenuF.setLocationByPlatform(真);mainMenuF.setVisible(true)。请参阅(是)。尝试使用
GridBagLayout
而不是
BoxLayout
,有关更多详细信息,请参阅
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();
    }
}