微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将 gridbaglayout 与组件对齐到它们所在的 JPanel 的左上角

我有 2 个带有 GridBagLayouts 的 JPanel 来设置布局。唯一的问题是组件都在面板的中心。

我需要这些组件集群与它们添加到的 JPanel 的左上角对齐。

我知道您可以使用锚变量对齐布局中的组件,但我希望整个网格锚定在面板的左上角

(我夸大了尺寸以说明问题所在)

enter image description here

示例工具对象:

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);
    private static final Insets TITLE_LABEL_INSETS = new Insets(5,4,0);
    private static final Insets FIELD_INSETS = new Insets(0,0);
    private static final Insets CHECKBox_INSETS = new Insets(0,48,0);
    private static final Font DEF_FONT = new Font("Default font",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",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",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,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);
        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,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);
        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,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);
    }
}

解决方法

您需要 GridBagConstraints 字段的值组合。特别是 anchorweightxweighty

下面的代码是一个简单的例子,显示了两行,每行包含一个 JLabel 和一个 JTextField。代码后的注释。

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。
  • 还将每行中的最后一个组件的 gridwidth 设置为 GridBagConstraints.REMAINDER
  • 将最后一行的组件的 weighty 设置为 1。

这是屏幕截图。

screen capture of running app

参考How to Use GridBagLayout

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。