我写了一个程序来生成PDF表格。为了更容易操作,我还创建了一个GUI来控制事物。但是,在JFrame上,这个JComboBox占用了大量空间,使得右侧子面板不必要地变宽,这让我感到困扰。
原因可能是comboBox中有一个很长的字符串。
我尝试使用"设置首选尺寸"面板,但它没有工作。然后我继续使用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");