1

我正在开发一个 JDialog(手动,没有 GUI 构建器),但我在布局时遇到了问题。
我有这个:
替代文字
我的问题是我不知道如何告诉 JList(在 JScrollPane 内)具有最大宽度,我使用了 setSize、setMaximumSize 并且没有任何效果!我需要 JList 的宽度是图片大小的一半。

解释布局:
“基因信息”是一个 GridLayout 2x4,它包含在一个带有 BoxLayout 的 JPanel 中,+/- JButtons 也是一个 BoxLayout,我之前所说的都在一个 BoxLayout 中。现在,“基因”JPanel 是一个 GridBagLayout。

我能做些什么?

提前致谢!

PD:其他的边界只是为了查看组件的边界。

Source Code:

scpGenesList.setViewportView(lstGenesList);

pnlGeneInfo.setLayout(new GridLayout(4, 2, 10, 10));
pnlGeneInfo.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Gene Information"), 
        BorderFactory.createEmptyBorder(10, 10, 10, 10)));
lblGeneSymbol.setText("Symbol:");
lblGeneSymbol.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneChromosome.setText("Chromosome:");
lblGeneChromosome.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneStartPosition.setText("Start Position:");
lblGeneStartPosition.setHorizontalAlignment(SwingConstants.RIGHT);
lblGeneStopPosition.setText("Stop Position:");
lblGeneStopPosition.setHorizontalAlignment(SwingConstants.RIGHT);
pnlGeneInfo.add(lblGeneSymbol);
pnlGeneInfo.add(lblGeneSymbolValue);
pnlGeneInfo.add(lblGeneChromosome);
pnlGeneInfo.add(lblGeneChromosomeValue);
pnlGeneInfo.add(lblGeneStartPosition);
pnlGeneInfo.add(lblGeneStartPositionValue);
pnlGeneInfo.add(lblGeneStopPosition);
pnlGeneInfo.add(lblGeneStopPositionValue);

pnlGWASAddRemoveButtons.setLayout(new BoxLayout(pnlGWASAddRemoveButtons, BoxLayout.X_AXIS));
pnlGWASAddRemoveButtons.add(Box.createHorizontalGlue());
pnlGWASAddRemoveButtons.add(cmdGenesAdd);
pnlGWASAddRemoveButtons.add(Box.createHorizontalStrut(10));
pnlGWASAddRemoveButtons.add(cmdGenesRemove);
pnlGWASAddRemoveButtons.add(Box.createHorizontalGlue());

pnlGeneInfoButtons.setLayout(new BoxLayout(pnlGeneInfoButtons, BoxLayout.Y_AXIS));
pnlGeneInfoButtons.add(pnlGeneInfo);
pnlGeneInfoButtons.add(Box.createVerticalStrut(10));
pnlGeneInfoButtons.add(pnlGWASAddRemoveButtons);

pnlGenesPanel.setLayout(new GridBagLayout());
pnlGenesPanel.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Genes"),
        BorderFactory.createEmptyBorder(10, 10, 10, 10)));

GridBagConstraints ctrGenes = new GridBagConstraints();
ctrGenes.fill = GridBagConstraints.BOTH;
ctrGenes.gridx = 0;
ctrGenes.gridy = 0;
ctrGenes.gridwidth = 1;
ctrGenes.gridheight = 1;
ctrGenes.weighty = 1.0;
ctrGenes.weightx = 1.0;
ctrGenes.insets = new Insets(0, 0, 0, 10);
pnlGenesPanel.add(scpGenesList, ctrGenes);

GridBagConstraints ctrGenesInfoButton = new GridBagConstraints();
ctrGenesInfoButton.fill = GridBagConstraints.BOTH;
ctrGenesInfoButton.gridx = 1;
ctrGenesInfoButton.gridy = 0;
ctrGenesInfoButton.gridwidth = 1;
ctrGenesInfoButton.gridheight = 1;
ctrGenesInfoButton.weighty = 1.0;
ctrGenesInfoButton.weightx = 1.0;
pnlGenesPanel.add(pnlGeneInfoButtons, ctrGenesInfoButton);

contentPane.add(pnlGenesPanel);

pack();

4

7 回答 7

3

为什么不给“基因”面板一个 2x1 GridLayout?这应该确保双方具有相同的大小。

但实际上,给列表提供控件未占用的所有空间对我来说更有意义,因为它们需要固定数量的空间,而如果有宽条目,列表可能会受益于它可以获得的所有额外空间。

为此,我会给“基因”面板 a BorderLayout,将列表放在 CENTER 插槽中,将控件放在 EAST 插槽中。

于 2011-01-18T19:39:18.723 回答
2

按照@Michael Borgwardt 的建议让列表增长,您可以使用它setVisibleRowCount()来生成方便的初始面板大小。如有必要,您还可以检查Dimension返回的 by ,它“计算显示行getPreferredScrollableViewportSize()所需的视口大小”。visibleRowCount

于 2011-01-18T21:27:36.350 回答
1

它没有回答您的问题 - 但我发现 JGoodies FormLayout 比 GridBagLayout 更直观。可以在此处找到该库以及一些示例:

http://jgoodies.com/freeware/forms/index.html

于 2011-01-18T20:19:55.263 回答
1

我认为早期的解决方案都是有效的,而且在使用哪些布局管理器方面更多的是一种编码偏好。根据您的要求,这是一个仅使用标准布局管理器(网格、GridBag 和边框)的工作。玩得开心,-MS。

导入 java.awt。; 导入 javax.swing。; 导入 javax.swing.border.*;

公共类 GeneDialog 扩展 JDialog {

private String[]    plusMinus = {"+","-"}, tfNames = {
                "Symbol", "Chromosome", "Start position", "Stop position"},
            listData = {"Gene01", "Gene02", "Gene03", "Gene04", "Gene05", "Gene06",
                "Gene07", "Gene08", "Gene09", "Gene10", "Gene11", "Gene12"};
private JTextField[]    gtField= new JTextField[tfNames.length];
private JList       list = new JList (new DefaultListModel());

    public GeneDialog (Frame f, String title) {
    super (f, title, true);
    Container cp = getContentPane();
    cp.setLayout (new GridLayout(1,2));
    JScrollPane listScrollPane = new JScrollPane (list);
    listScrollPane.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createTitledBorder("Genes"),
                    BorderFactory.createEmptyBorder(10, 10, 10, 10))); 
    DefaultListModel lm = (DefaultListModel) list.getModel();
    for (int k = 0 ; k < listData.length ; k++)
        lm.addElement (listData[k]);
    cp.add (listScrollPane);
    cp.add (controlPanel());
    pack();
    }

private GridBagConstraints makeGBC (int inset) {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets (inset, inset, inset, inset);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = GridBagConstraints.RELATIVE;
    return gbc;
}

private JPanel controlPanel() {
    JPanel cp = new JPanel (new BorderLayout()),
           bp = new JPanel (new GridBagLayout()),
           tp = new JPanel (new GridBagLayout());

    GridBagConstraints gbc = makeGBC (10);
    for (int i = 0 ; i < tfNames.length ; i++) {
        JLabel label = new JLabel (tfNames[i], JLabel.TRAILING);
        tp.add (label, gbc);
    }
    gbc.gridx++; gbc.weightx = 1.0f;
    for (int i = 0 ; i < tfNames.length ; i++) {
        gtField[i] = new JTextField(12);
        tp.add (gtField[i], gbc);
    }

    gbc = makeGBC (10);
    for (int i = 0 ; i < plusMinus.length ; i++) {
        JButton b = new JButton (plusMinus[i]);
        bp.add (b, gbc);
        gbc.gridx++;
    }

    cp.add (tp, "Center");
    cp.add (bp, "South");
    return cp;
}

public static void main (String[] args) {
    new GeneDialog (null, "Genes").setVisible (true);

}}

于 2011-01-19T06:29:24.623 回答
1

如果没有看到这里的所有代码,可能无法告诉你哪里出了问题。如果您有时间,我建议您看看MigLayout。您可以将它与 Swing 和 SWT 一起使用,一旦您了解它,恕我直言,它是一个非常强大的布局管理器。

希望这有帮助,祝你好运。

于 2011-01-18T18:32:23.760 回答
0

JScrollPane尝试为其中的 the和JListthat设置最大宽度。

于 2011-01-18T19:12:53.103 回答
0

我只想告诉我,我和 javamonkey79 有同样的看法。看看 MigLayout,你会喜欢的,从 Java 7 开始,它将成为标准的 java-onboard 布局。

于 2011-01-18T19:29:22.520 回答