如何将gridbaglayout与它们所在的jpanel左上角的组件对齐

wdebmtf2  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(231)

我有2个带gridbaglayouts的JPanel来设置布局。唯一的问题是部件都在面板的中心。
我需要这些组件集群与它们添加到的JPanel的左上角对齐。
我知道可以使用锚定变量在布局中对齐组件,但我希望将整个网格锚定到面板的左上角。
(我夸大了尺寸,以说明问题所在)

示例工具对象:

package UserInterfaces.Sample.Interfaces;

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

/**
 *
 * @author Edward Jenkins
 */
public class SampleTools extends JPanel {

    // constants
    private static final Dimension VALUE_SPINNER_SIZE = new Dimension(45, 20);
    private static final Dimension DETAILS_TEXT_FIELD_SIZE
            = new Dimension(180, 20);
    private static final Dimension SAMPLE_DETAILS_SIZE = new Dimension(305, 135);
    private static final Dimension SOUND_OPTIONS_SIZE = new Dimension(355, 135);
    private static final Insets DEF_INSETS = new Insets(0, 0, 0, 0);
    private static final Insets TITLE_LABEL_INSETS = new Insets(5, 4, 0, 0);
    private static final Insets FIELD_INSETS = new Insets(0, 4, 0, 0);
    private static final Insets CHECKBOX_INSETS = new Insets(0, 48, 0, 0);
    private static final Font DEF_FONT = new Font("Default font", 0, 12);
    private static final Font BOLD_FONT = new Font("Bold font", 1, 12);
    private static final Font ITALIC_FONT = new Font("Italic font", 2, 12);

    // instance variables
    // this panel
    private JToolBar sammpleToolBar;
    private GridBagLayout toolsLayout;
    private GridBagConstraints tc;
    // basics
    // Sample details (File name, bitrate, channels, etc.)
    private JPanel sampleDetails;
    private GridBagLayout sampleDetailsLayout;
    private GridBagConstraints sdc;
    private Border sampleDetailsBorder;
    private JLabel sampleNameLabel;
    private JTextField sampleNameField;
    private JLabel fileNameLabel;
    private JTextField fileNameField;
    private JLabel sampleFormatTitleLabel;
    private JLabel sampleFormatLabel;
    private JLabel sampleLengthTitleLabel;
    private JLabel sampleLengthLabel;
    // sound options (volume, panning, etc.)
    private JPanel soundOptions;
    private GridBagLayout soundOptionsLayout;
    private GridBagConstraints soc;
    private Border soundOptionsBorder;
    private JLabel defaultVolumeLabel;
    private JSpinner defaultVolumeValue;
    private SpinnerModel defVolumeSpinnerModel;
    private JSlider defaultVolumeSlider;
    private JLabel globalVolumeLabel;
    private JSpinner globalVolumeValue;
    private SpinnerModel globalVolumeSpinnerModel;
    private JSlider globalVolumeSlider;
    private JLabel defaultPanningLabel;
    private JSpinner defaultPanningValue;
    private SpinnerModel panSpinnerModel;
    private JSlider defaultPanningSlider;
    private JLabel usePanningLabel;
    private JCheckBox panning;
    private JLabel useSurroundLabel;
    private JCheckBox surround;
    // sampling tools (definging note frequency and C5 frequency)
    private JPanel samplingTools;
    private JTextField c5SampleRate;
    private JComboBox sampleNotesCombo;
    private JSpinner fineTuneSpinner;
    private JPanel loopingTools;
    private JPanel vibratoOptions;
    private JPanel detuningOptins;
    private JPanel ADSROptions;
    private JButton resampleButton;

    // consturctor
    public SampleTools() {

        // set layout
        toolsLayout = new GridBagLayout();
        tc = new GridBagConstraints();
        tc.anchor = GridBagConstraints.NORTHWEST;
        this.setLayout(toolsLayout);

        // set the panels
        createSampleDetailsPanel();
        createSoundOptionsPanel();
        sampleDetails.setPreferredSize(SAMPLE_DETAILS_SIZE);
        soundOptions.setPreferredSize(SOUND_OPTIONS_SIZE);
        //createSampleToolsPanel();
    }

    private void createSampleDetailsPanel() {

        // set the layout
        sampleDetailsLayout = new GridBagLayout();
        sdc = new GridBagConstraints();
        sdc.anchor = GridBagConstraints.WEST;

        // set the border
        sampleDetailsBorder
                = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);

        // set the border title
        sampleDetailsBorder
                = BorderFactory.createTitledBorder(sampleDetailsBorder,
                        "Sample details", 0, 0, BOLD_FONT);

        // set the sample options JPanel
        sampleDetails = new JPanel(sampleDetailsLayout, false);

        // set details border
        sampleDetails.setBorder(sampleDetailsBorder);

        // set the sample name label
        sampleNameLabel = new JLabel("Sample Name: ");
        sampleNameLabel.setFont(DEF_FONT);
        sdc.gridx = 0;
        sdc.gridy = 0;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(sampleNameLabel, sdc);

        // set the sample name field
        sampleNameField = new JTextField("");
        sampleNameField.setToolTipText("Name of sample. ");
        sampleNameField.setPreferredSize(DETAILS_TEXT_FIELD_SIZE);
        sdc.gridx = 1;
        sdc.gridy = 0;
        sdc.insets = FIELD_INSETS;
        sampleDetails.add(sampleNameField, sdc);

        // set the file name label
        fileNameLabel = new JLabel("Sample file Name: ");
        fileNameLabel.setFont(DEF_FONT);
        sdc.gridx = 0;
        sdc.gridy = 1;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(fileNameLabel, sdc);

        // set the file name field
        fileNameField = new JTextField("");
        fileNameField.setToolTipText("File name of sample. "
                + "Mostly used with s3m samples.");
        fileNameField.setPreferredSize(DETAILS_TEXT_FIELD_SIZE);
        sdc.gridx = 1;
        sdc.gridy = 1;
        sdc.insets = FIELD_INSETS;
        sampleDetails.add(fileNameField, sdc);

        // set the sample format title label
        sampleFormatTitleLabel = new JLabel("Sample format: ");
        sampleFormatTitleLabel.setFont(DEF_FONT);
        sdc.gridx = 0;
        sdc.gridy = 2;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(sampleFormatTitleLabel, sdc);

        // set the sample format label
        sampleFormatLabel = new JLabel("Signed 16-bit, sterero ");
        sampleFormatLabel.setFont(ITALIC_FONT);
        sdc.gridx = 1;
        sdc.gridy = 2;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(sampleFormatLabel, sdc);

        // set the sample length title label
        sampleLengthTitleLabel = new JLabel("Sample length: ");
        sampleLengthTitleLabel.setFont(DEF_FONT);
        sdc.gridx = 0;
        sdc.gridy = 3;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(sampleLengthTitleLabel, sdc);

        // set the sample length label
        sampleLengthLabel = new JLabel("65535");
        sampleLengthLabel.setFont(ITALIC_FONT);
        sdc.gridx = 1;
        sdc.gridy = 3;
        sdc.insets = TITLE_LABEL_INSETS;
        sampleDetails.add(sampleLengthLabel, sdc);

        // add to tools;
        tc.gridx = 0;
        this.add(sampleDetails, tc);
    }

    private void createSoundOptionsPanel() {

        // set the layout
        soundOptionsLayout = new GridBagLayout();
        soc = new GridBagConstraints();
        soc.anchor = GridBagConstraints.WEST;

        // set the border
        soundOptionsBorder
                = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);

        // set the border title
        soundOptionsBorder
                = BorderFactory.createTitledBorder(soundOptionsBorder,
                        "Sound options", 0, 0, BOLD_FONT);

        // set the sample options JPanel
        soundOptions = new JPanel(soundOptionsLayout, false);

        // set options border
        soundOptions.setBorder(soundOptionsBorder);

        // set the default volume label
        defaultVolumeLabel = new JLabel("Default volume: ");
        defaultVolumeLabel.setFont(DEF_FONT);
        soc.gridx = 0;
        soc.gridy = 0;
        soc.insets = TITLE_LABEL_INSETS;
        soundOptions.add(defaultVolumeLabel, soc);

        // set default volume spinner model
        defVolumeSpinnerModel = new SpinnerNumberModel(64, 0, 64, 1);

        // set the default volume value spinner
        defaultVolumeValue = new JSpinner(defVolumeSpinnerModel);
        defaultVolumeValue.setPreferredSize(VALUE_SPINNER_SIZE);
        soc.gridx = 1;
        soc.gridy = 0;
        soc.insets = DEF_INSETS;
        soundOptions.add(defaultVolumeValue, soc);

        // set the default volume slider
        defaultVolumeSlider = new JSlider(0, 64, 64);
        soc.gridx = 2;
        soc.gridy = 0;
        soundOptions.add(defaultVolumeSlider, soc);

        // set the global volume label
        globalVolumeLabel = new JLabel("Global volume: ");
        globalVolumeLabel.setFont(DEF_FONT);
        soc.gridx = 0;
        soc.gridy = 1;
        soc.insets = TITLE_LABEL_INSETS;
        soundOptions.add(globalVolumeLabel, soc);

        // set global volume spinner model
        globalVolumeSpinnerModel = new SpinnerNumberModel(64, 0, 64, 1);

        // set the global volume value spinner
        globalVolumeValue = new JSpinner(globalVolumeSpinnerModel);
        globalVolumeValue.setPreferredSize(VALUE_SPINNER_SIZE);
        soc.gridx = 1;
        soc.gridy = 1;
        soc.insets = DEF_INSETS;
        soundOptions.add(globalVolumeValue, soc);

        // set the global volume slider
        globalVolumeSlider = new JSlider(0, 64, 64);
        soc.gridx = 2;
        soc.gridy = 1;
        soundOptions.add(globalVolumeSlider, soc);

        // set the use panning label
        usePanningLabel = new JLabel("Use panning: ");
        usePanningLabel.setFont(DEF_FONT);
        soc.gridx = 0;
        soc.gridy = 2;
        soc.insets = TITLE_LABEL_INSETS;
        soundOptions.add(usePanningLabel, soc);

        // set the use panning checkbox
        panning = new JCheckBox();
        panning.setSelected(false);
        soc.gridx = 1;
        soc.gridy = 2;
        soc.gridwidth = 2;
        soc.insets = CHECKBOX_INSETS;
        soundOptions.add(panning, soc);

        // set the panning spinner model
        panSpinnerModel = new SpinnerNumberModel(0, -128, 127, 1);

        // set the default panning label
        defaultPanningLabel = new JLabel("Default panning: ");
        defaultPanningLabel.setFont(DEF_FONT);
        soc.gridx = 0;
        soc.gridy = 3;
        soc.gridwidth = 1;
        soc.insets = TITLE_LABEL_INSETS;
        soundOptions.add(defaultPanningLabel, soc);

        // set the default panning value spinner
        defaultPanningValue = new JSpinner(panSpinnerModel);
        defaultPanningValue.setPreferredSize(VALUE_SPINNER_SIZE);
        defaultPanningValue.setEnabled(panning.isSelected());
        soc.gridx = 1;
        soc.gridy = 3;
        soc.insets = DEF_INSETS;
        soundOptions.add(defaultPanningValue, soc);

        // set the default panning slider
        defaultPanningSlider = new JSlider(-128, 127, 0);
        defaultPanningSlider.setEnabled(panning.isSelected());
        soc.gridx = 2;
        soc.gridy = 3;
        soundOptions.add(defaultPanningSlider, soc);

        // set the use surround label
        useSurroundLabel = new JLabel("Use surround: ");
        useSurroundLabel.setFont(DEF_FONT);
        soc.gridx = 0;
        soc.gridy = 4;
        soc.insets = TITLE_LABEL_INSETS;
        soundOptions.add(useSurroundLabel, soc);

        // set the use surround checkbox
        surround = new JCheckBox();
        surround.setSelected(false);
        soc.gridx = 1;
        soc.gridy = 4;
        soc.gridwidth = 2;
        soc.insets = CHECKBOX_INSETS;
        soundOptions.add(surround, soc);

        // add to tools
        tc.gridx = 1;
        this.add(soundOptions, tc);
    }

    private void createSampleToolsPanel() {

    }
}

主要方法:

/**
 *
 * @author Edward Jenkins
 */
public class testUIPanel {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        JFrame testFrame = new JFrame();
        testFrame.setLocationRelativeTo(null);
        testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testFrame.setTitle("Test User Interface");
        testFrame.add(new SampleTools());
        testFrame.pack();
        testFrame.setVisible(true);
    }
}
jgovgodb

jgovgodb1#

您需要gridbagconstraints字段的值组合。特别地 anchorweightxweighty .
下面的代码是一个简单的示例,显示了两行,其中每行包含一个 JLabelJTextField . 代码后面的注解。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SampleTools {
    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createSampledetailsPanel());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createSampledetailsPanel() {
        JPanel sampledetailsPanel = new JPanel(new GridBagLayout());
        sampledetailsPanel.setPreferredSize(new Dimension(500, 300));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets.left = 10;
        gbc.insets.top = 10;
        JLabel nameLabel = new JLabel("Sample Name");
        sampledetailsPanel.add(nameLabel, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        JTextField nameTextField = new JTextField(10);
        sampledetailsPanel.add(nameTextField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 1.0;
        JLabel fileLabel = new JLabel("Sample File");
        sampledetailsPanel.add(fileLabel, gbc);
        gbc.gridx = 1;
        JTextField fileTextField = new JTextField(10);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1.0;
        sampledetailsPanel.add(fileTextField, gbc);
        return sampledetailsPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new SampleTools().createAndDisplayGui());
    }
}

这个 anchor 设置将每个组件放置在其单元格的左上角。
设置 weightx 对于每行中的最后一个零部件,设置为1。
也设置 gridwidthGridBagConstraints.REMAINDER 对于每行中的最后一个组件。
设置 weighty 对于最后一行中的组件,将其设置为1。
这是一个屏幕截图。

请参阅如何使用gridbaglayout

相关问题