Java 使用 GridBagLayout 限制 JPanel 的宽度

Java Restraining width for JPanel with GridBagLayout

我写了一个程序来生成 PDF 表格。为了更容易操作,我还创建了一个 GUI 来控制这些东西。然而,让我很困扰的是,在 JFrame 上,这个 JComboBox 占用了很多空间 space,使右侧的子面板不必要地变宽。

原因可能是comboBox中有一个很长的字符串

我尝试对面板使用 "set preferred size",但没有成功。然后我继续使用 GridBagLayout 来尝试缩小该组合框。有没有办法将所有按钮和组合框限制在一定宽度?或者我应该使用不同的布局?

最好是右侧面板中的所有组件都具有相同的宽度,就像它们现在一样。

谢谢!

这是我的代码:

JTable studentTable=new JTable(new StudentTableModel(bro));
            studentTable.setRowHeight(25);
            studentTable.setGridColor(java.awt.Color.BLACK);
            DefaultTableCellRenderer centerRenderer = new     
DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

studentTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(3).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(5).setCellRenderer(centerRenderer);
        JPanel panelA1= new JPanel();
            panelA1.setOpaque(false);
            panelA1.setAlignmentX(Component.LEFT_ALIGNMENT);
            panelA1.setLayout(new GridBagLayout());

        JScrollPane scrollPaneA= new JScrollPane(studentTable);

        JButton buttonA1= new JButton("Activate All");

        JButton buttonA2= new JButton("Create Tables");

        JButton buttonA3= new JButton("Save File");

        JLabel labelA1= new JLabel("-Job Crews-");

        JButton buttonA4= new JButton("Deact Crew");

        JComboBox<Object> comboBoxA1=new JComboBox<Object>();
        comboBoxA1.addItem(new String("-N/A-"));
        for(JobCrew jj: crews)
        {
            comboBoxA1.addItem(jj);
        }
        comboBoxA1.setMaximumSize(new Dimension(100,100));

        GridBagConstraints c = new GridBagConstraints();
        {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            panelA1.add(buttonA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            panelA1.add(buttonA2,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            panelA1.add(buttonA3,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            panelA1.add(labelA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 4;
            panelA1.add(comboBoxA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 5;
            panelA1.add(buttonA4,c);

        }

        JPanel panelA= new JPanel();
            panelA.setLayout(new BoxLayout(panelA,BoxLayout.X_AXIS));
            panelA.setOpaque(false);
            panelA.add(scrollPaneA); panelA.add(panelA1);
        //////
        /**********/
        tabs.addTab("Student info",null,panelA,"Organizes & displays student information");

查看 Combo Box Popup

它将允许您控制组合框本身的宽度,弹出框可以是全宽。

或者您可以设置 属性 以便在需要时在弹出窗口中显示滚动条。