java - 我不明白为什么我的 GridBagLayout 不遵循规则

标签 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 的其他部分。

最佳答案

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);
        }

    }

}

为了帮助减少 UI 的困惑,您可以考虑使用 JToolBar 而不是底部边缘的所有按钮,请参阅 How to Use Tool Bars了解更多详情

关于java - 我不明白为什么我的 GridBagLayout 不遵循规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707193/

相关文章:

java - 使用 SQLiteQueryBuilder 生成 UPDATE 查询

Java JTable 分组

java - 如何使用 .hasMoreElements() 循环 JButtonGroup?

java - 绘制嵌套椭圆并枚举它们

java - 嵌套 GridBagLayout - 如何将子级的列与父级的列对齐

java - 向程序添加列表选择监听器时卡住了

java - 将任意数据与 ejb 调用上下文关联

java - 用于 mongodb 的 ycsb

java - 需要编辑条形图的 Swing GUI 代码

java - 查询数据一次并在整个应用程序中像缓存一样使用它