在JPanel中创建JComboBoxes时,是否可以控制每个JComboBox的大小(特别是高度)

时间:2014-06-04 16:49:55

标签: java javascript swing jpanel jcombobox

我创建了JPanel,其中我创建了不同数量的JComboBoxes。放入JComboBoxes的数量取决于不同的条件。

我遇到的问题是,当我创建这些JComboBoxes时,它们会自动适应JPanel的完整尺寸,请参阅下面的图片。

enter image description hereImage where just one <code>JComboBox</code> is created which takes up the whole JPanel()

我有什么方法可以修正此JComboBox的大小,使其小于JPanel的完整尺寸?


关注信息


感谢您的关注和帮助

我正在使用Netbeans开发工具。我首先创建了一个JScrollPanel,然后使用NetBeans将JPanel放在这个JScrollPanel中,并在initComponents()中用不可编辑的代码创建它们。

我可以为此提供所有代码,但它数量可能很大,可能没用,它开始:

    jScrollPane = new javax.swing.JScrollPane();
    isotopePanel = new javax.swing.JPanel();

    jScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Isotope Details"));

    isotopePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
    isotopePanel.setAutoscrolls(true);

    javax.swing.GroupLayout isotopePanelLayout = new javax.swing.GroupLayout(isotopePanel);
    isotopePanel.setLayout(isotopePanelLayout);
    isotopePanelLayout.setHorizontalGroup(
        isotopePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 755, Short.MAX_VALUE)
    );
    isotopePanelLayout.setVerticalGroup(
        isotopePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 356, Short.MAX_VALUE)
    );

    jScrollPane.setViewportView(isotopePanel);

    .....

在我的代码中,在aboutToDisplay()中,我在名为LabelsAndCombos的数组中为它们创建了JComboBoxe和标签,然后使用代码将它们放入Jpanel:

    javax.swing.GroupLayout isotopesLayout = new javax.swing.GroupLayout(isotopePanel);   

    isotopesLayout.setAutoCreateGaps(true);
    isotopesLayout.setAutoCreateContainerGaps(true);

    javax.swing.GroupLayout.SequentialGroup hGroup = isotopesLayout.createSequentialGroup();
    javax.swing.GroupLayout.SequentialGroup vGroup = isotopesLayout.createSequentialGroup();

    GroupLayout.ParallelGroup labelGroup = isotopesLayout.createParallelGroup();
    GroupLayout.ParallelGroup comboBoxGroup = isotopesLayout.createParallelGroup();
    for(int i=0;i<calcDef.getAtomLabels().length;i++){
        labelGroup.addComponent(labelsAndCombos[i].label);
        comboBoxGroup.addComponent(labelsAndCombos[i].combo);
        vGroup.addGroup(isotopesLayout.createParallelGroup().addComponent(labelsAndCombos[i].label).addComponent(labelsAndCombos[i].combo));
    }

    hGroup.addGroup(labelGroup);
    hGroup.addGroup(comboBoxGroup);

    isotopesLayout.setHorizontalGroup(hGroup);
    isotopesLayout.setVerticalGroup(vGroup);

    isotopePanel.setLayout(isotopesLayout);

如果我能提供更多信息以便更清楚,请告诉我。

谢谢

2 个答案:

答案 0 :(得分:0)

您可能正在使用GridLayout来规定面板中元素的大小。请考虑在JPanel中使用不同的布局,例如GridBagLayout。例如:

An example of GridBagLayout with a JCombobox that does not vertically resize

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.awt.Insets;


public class IsotopeDetails extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    IsotopeDetails frame = new IsotopeDetails();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public IsotopeDetails() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{0, 0, 0};
        gbl_panel.rowHeights = new int[]{0, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblIsotopeDetails = new JLabel("Isotope Details");
        GridBagConstraints gbc_lblIsotopeDetails = new GridBagConstraints();
        gbc_lblIsotopeDetails.insets = new Insets(0, 0, 0, 5);
        gbc_lblIsotopeDetails.anchor = GridBagConstraints.EAST;
        gbc_lblIsotopeDetails.gridx = 0;
        gbc_lblIsotopeDetails.gridy = 0;
        panel.add(lblIsotopeDetails, gbc_lblIsotopeDetails);

        JComboBox comboBox = new JComboBox();
        GridBagConstraints gbc_comboBox = new GridBagConstraints();
        gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
        gbc_comboBox.gridx = 1;
        gbc_comboBox.gridy = 0;
        panel.add(comboBox, gbc_comboBox);

        pack();
    }

}

答案 1 :(得分:0)

使用适当的布局管理器来获得所需的布局。阅读Using Layout Manager上Swing教程中的部分以获取更多信息和示例。一些布局管理器会尊重组件的大小,而其他布局管理器则不会。

您仍然可以将GridLayout用于一个面板,然后将该面板嵌套在使用FlowLayout的另一个面板中。类似的东西:

JPanel comboBoxes = new JPanel( new GridLayout(0, 1) );
comboBoxes.add( comboBox1 );
comboBoxes.add( comboBox2 );

JPanel wrapper = new JPanel();
wrapper.add( comboBoxes );

frame.add( wrapper );

您选择的布局管理器取决于您的确切要求。