GridBagLayout似乎锚定在底部,并扩展标签

时间:2015-09-27 12:02:17

标签: java swing layout-manager gridbaglayout

我是GBL的新手,请原谅我的新手问题:

当我展开窗口时,网格的Y尺寸变大,我不想要,而且我的标签也在x轴上增长。这是我希望窗口看起来像任何时候: Screenshot

但它看起来像这样: Screenshot

这是我的代码:

package fr.oxodao.youtump3;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Nathan J. <Oxodao> on 27/09/15 @ 13:26.
 */
public class PanelAdd extends JPanel {
public JTextField destination;

public PanelAdd() {
    setLayout(new BorderLayout());
    JComboBox serviceChoose = new JComboBox(Main.downloadServices.toArray());
    JPanel topPan = new JPanel(new BorderLayout());
    topPan.add(serviceChoose, BorderLayout.CENTER);
    topPan.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
    serviceChoose.setSelectedIndex(0);
    add(topPan, BorderLayout.NORTH);

    JPanel centerPanel = new JPanel(new BorderLayout());
    JPanel chooseFolder = new JPanel(new BorderLayout());
    {
        chooseFolder.add(new JLabel("Emplacement: "), BorderLayout.WEST);
        chooseFolder.add(destination = new JTextField(/** @TODO Load From Database  **/), BorderLayout.CENTER);
        JButton select = new JButton("Choisir");
        select.addActionListener(e -> {
        });
        chooseFolder.add(select, BorderLayout.EAST);
    }
    centerPanel.add(chooseFolder, BorderLayout.NORTH);

    JPanel id3 = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.LINE_START;
    c.fill = GridBagConstraints.NONE;
    JLabel labL1 = new JLabel("Lien youtube: ");
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    id3.add(labL1, c);
    JLabel lab1 = new JLabel("Titre: ");
    c.gridy = 1;
    id3.add(lab1, c);

    JLabel lab2 = new JLabel("Auteur: ");
    c.gridy = 2;
    id3.add(lab2, c);

    JLabel lab3 = new JLabel("Album: ");
    c.gridy = 3;
    id3.add(lab3, c);

    JLabel lab4 = new JLabel("/");
    c.gridx = 3;
    id3.add(lab4, c);

    JTextField fLien = new JTextField();
    JTextField fTitre = new JTextField();
    JTextField fAuteur = new JTextField();
    JTextField fAlbum = new JTextField();
    JTextField fPiste = new JTextField();
    JTextField fTotal = new JTextField();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridy = 0;
    c.gridx = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    id3.add(fLien, c);
    c.gridy = 1;
    id3.add(fTitre, c);

    c.gridy = 2;
    id3.add(fAuteur, c);

    c.gridy = 3;
    c.gridwidth = 1;
    id3.add(fAlbum, c);

    c.gridx = 2;
    id3.add(fPiste, c);

    c.gridx = 4;
    id3.add(fTotal, c);

    id3.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0));
    centerPanel.add(id3, BorderLayout.CENTER);
    centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 15, 5, 15));

    JPanel btmBtn = new JPanel();
    btmBtn.setLayout(new GridBagLayout());
    c = new GridBagConstraints();

    JButton resetBt = new JButton("Effacer tout");
    resetBt.addActionListener(e -> {
    });
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(0, 0, 0, 1);
    btmBtn.add(resetBt, c);

    JButton addBt = new JButton("Ajouter aux téléchargements");
    addBt.addActionListener(e -> {
    });
    c.anchor = GridBagConstraints.LINE_END;
    c.gridx = 1;
    c.insets = new Insets(0, 2, 0, 0);
    btmBtn.add(addBt, c);

    add(btmBtn, BorderLayout.SOUTH);
    add(centerPanel, BorderLayout.CENTER);
}
}

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用:

c.weighty = 0.0;

并且您的网格不会与其父网格垂直增长。

要将面板放在父容器的顶部,请使用:

centerPanel.add(id3, BorderLayout.NORTH);