我无法弄清楚为什么我的GridBagLayout没有遵守规则

时间:2016-04-19 01:58:03

标签: java swing

我一直在努力使造型工作,但我无法弄清楚为什么应该存在的gridbagconstraints不起作用。它在某种程度上起作用,但它也只是不起作用......具体的代码在这里:

private void initComponents() {



    usernameLabel = new JLabel();
    schedule = new CourseList();
    addCourseButton = new JButton();
    deleteCourseButton = new JButton();
    printScheduleButton = new JButton();
    feesButton = new JButton();
    helpLink = new SwingLink("help", "http://java.sun.com");
    logoutButton = new JButton();
    String u = getUsername();
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConstraints gbc;

    //Set Layout
    this.setLayout(gridBag);


    //Row One
    usernameLabel.setText("Welcome " + u);
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(20,10,10,10); //top, left, bottom, right
    gbc.anchor = gbc.LINE_END;
    gbc.fill = gbc.BOTH;
    this.add(usernameLabel, gbc);

    //Rows 2-4
    schedule.getAccessibleContext().setAccessibleName("Course Schedule");
    gbc = new GridBagConstraints();
    gbc.gridheight = 3;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.insets = new Insets(10,10,10,10); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.BOTH;
    this.add(schedule, gbc);

    //Row 5 - empty label
    JLabel hiddenLabel1 = new JLabel();
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.insets = new Insets(10,10,10,10); //top, left, bottom, right
    gbc.fill = gbc.BOTH;
    this.add(hiddenLabel1, gbc);

    //Row 6
    addCourseButton.setText("Register");
    addCourseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            addCourseActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,10,10,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.HORIZONTAL;
    this.add(addCourseButton, gbc);

    deleteCourseButton.setText("Drop Delete Course");
    deleteCourseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            deleteCourseActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,5,10,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.HORIZONTAL;
    this.add(deleteCourseButton, gbc);


    printScheduleButton.setText("Print Schedule");
    printScheduleButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            printScheduleActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 4;
    gbc.gridy = 5;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,5,10,10); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.HORIZONTAL;
    this.add(printScheduleButton, gbc);


    //Row 7
    feesButton.setText("fees");
    feesButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            feesButtonActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,10,20,5); //top, left, bottom, right
    gbc.anchor = gbc.LINE_START; 
    gbc.fill = gbc.HORIZONTAL;
    this.add(feesButton, gbc);


    //Help Link postioning
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 2;
    gbc.gridy = 6;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,5,20,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.HORIZONTAL;
    helpLink.setCursor(new Cursor(Cursor.HAND_CURSOR));
    this.add(helpLink, gbc);

    logoutButton.setText("Logout");
    logoutButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            logoutButtonActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.gridx = 4;
    gbc.gridy = 6;
    gbc.ipadx = 10;
    gbc.insets = new Insets(10,5,20,10); //top, left, bottom, right
    gbc.anchor = gbc.LINE_END; 
    gbc.fill = gbc.HORIZONTAL;
    this.add(logoutButton, gbc);

    setBorder(BorderFactory.createTitledBorder(new MatteBorder(null), "", TitledBorder.CENTER, TitledBorder.TOP, new Font("Tahoma", 1, 14), new Color(255, 255, 255))); // NOI18N
    getAccessibleContext().setAccessibleName("Student Panel Welcome");

    setOpaque(false);
}  

当我运行它时,布局就像这样绘制:code running as written
一切都应该集中在一起,看起来更像是这样:design guide I wrote on a piece of paper in a meeting of sorts

如果需要任何其他代码来解决问题,请告诉我。我确定这是我的一些愚蠢的错误,但我不能为我的生活找到任何错误。对不起,如果这是一个微不足道的问题,它只会让我永远烦恼,我需要继续使用GUI的其他部分。

3 个答案:

答案 0 :(得分:2)

gridwidth可能很难处理,更好的方法可能是将布局分成组件并单独关注单个布局要求

Example

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JLabel title = new JLabel("Welcome");
            title.setHorizontalAlignment(JLabel.CENTER);
            add(title, BorderLayout.NORTH);

            TableModel model = new DefaultTableModel(new Object[]{"", "CRN", "Department", "Class", "Time", "Place"}, 5);
            add(new JScrollPane(new JTable(model)));

            JPanel options = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            options.add(new JButton("Add course"), gbc);
            gbc.gridx++;
            options.add(new JButton("Delete course"), gbc);
            gbc.gridx++;
            options.add(new JButton("Print Schedule"), gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            options.add(new JButton("Fees"), gbc);
            gbc.gridx++;
            JLabel help = new JLabel("Help");
            help.setHorizontalAlignment(JLabel.CENTER);
            options.add(help, gbc);
            gbc.gridx++;
            options.add(new JButton("Logout"), gbc);
            add(options, BorderLayout.SOUTH);
        }

    }

}

为了帮助减少用户界面的混乱,您可以考虑使用JToolBar代替底部边缘的所有按钮,有关详细信息,请参阅How to Use Tool Bars

答案 1 :(得分:1)

您似乎需要JPanel添加GridBagLayout

我发现这件事:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 希望有所帮助。

答案 2 :(得分:1)

所以我实际上只是在考虑它太难了。我需要给所有下部按钮提供相同的重量,以便它们正确对齐。我也不需要在大多数情况下使用填充。解决它的代码(如果它可以帮助任何人):

private void initComponents() {



    usernameLabel = new JLabel();
    schedule = new CourseList();
    addCourseButton = new JButton();
    deleteCourseButton = new JButton();
    printScheduleButton = new JButton();
    feesButton = new JButton();
    helpLink = new SwingLink("help", "http://java.sun.com");
    logoutButton = new JButton();
    String u = getUsername();
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConstraints gbc;

    //Set Layout
    this.setLayout(gridBag);


    //Row One
    usernameLabel.setText("<html><strong><u>Welcome </u></strong></html>" + u);
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(20,10,10,10); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    this.add(usernameLabel, gbc);

    //Rows 2-4
    schedule.getAccessibleContext().setAccessibleName("Course Schedule");
    gbc = new GridBagConstraints();
    gbc.gridheight = 3;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.insets = new Insets(10,10,10,10); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    gbc.fill = gbc.BOTH;
    this.add(schedule, gbc);

    //Row 5 - empty label
    JLabel hiddenLabel1 = new JLabel();
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 6;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.insets = new Insets(10,10,10,10); //top, left, bottom, right
    gbc.fill = gbc.BOTH;
    this.add(hiddenLabel1, gbc);

    //Row 6
    addCourseButton.setText("Register");
    addCourseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            addCourseActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,10,10,5); //top, left, bottom, right
    gbc.anchor = gbc.LINE_END;
    this.add(addCourseButton, gbc);

    deleteCourseButton.setText("Drop Delete Course");
    deleteCourseButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            deleteCourseActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,5,10,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    this.add(deleteCourseButton, gbc);


    printScheduleButton.setText("Print Schedule");
    printScheduleButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            printScheduleActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 4;
    gbc.gridy = 5;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,5,10,10); //top, left, bottom, right
    gbc.anchor = gbc.LINE_START;
    this.add(printScheduleButton, gbc);


    //Row 7
    feesButton.setText("fees");
    feesButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            feesButtonActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,10,20,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER; 
    this.add(feesButton, gbc);


    //Help Link postioning
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 2;
    gbc.gridy = 6;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,5,20,5); //top, left, bottom, right
    gbc.anchor = gbc.CENTER;
    helpLink.setCursor(new Cursor(Cursor.HAND_CURSOR));
    this.add(helpLink, gbc);

    logoutButton.setText("Logout");
    logoutButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            logoutButtonActionPerformed(evt);
        }
    });
    gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 2;
    gbc.weightx = 0.5;
    gbc.gridx = 4;
    gbc.gridy = 6;
    gbc.ipadx = 5;
    gbc.insets = new Insets(10,5,20,10); //top, left, bottom, right
    gbc.anchor = gbc.CENTER; 
    this.add(logoutButton, gbc);

    setBorder(BorderFactory.createTitledBorder(new MatteBorder(null), "", TitledBorder.CENTER, TitledBorder.TOP, new Font("Tahoma", 1, 14), new Color(255, 255, 255))); // NOI18N
    getAccessibleContext().setAccessibleName("Student Panel Welcome");

    setOpaque(false);
}

结果:running as correct