当前位置: 首页 > 知识库问答 >
问题:

如何将gridbag布局与它们所在的JPanel左上角的组件对齐

姜嘉赐
2023-03-14

我有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);
    }
}

共有1个答案

有骏奇
2023-03-14

您需要网格袋约束字段的值组合。特别是重量重量

下面的代码是一个简单的示例,显示了两行,每行包含一个< code>JLabel和一个< code>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());
    }
}
  • 锚点设置将每个组件放在其单元格的左上角。
  • 将每行中最后一个组件的 weightx 设置为 1。
  • 还要为每行中的最后一个组件设置网格宽度网格袋约束.REMAINDER
  • 最后一行中的组件的重量设置为 1。

这是一个屏幕截图。

参考如何使用网格包布局

 类似资料:
  • 我已经创建了一个简单的程序,它有1个JLabel说你想继续和2个JButtons,一个说是,一个说不。

  • 嘿,我正在尝试将一个面板中的所有面板对齐到较大面板的左侧。 以下是我目前面临的情况: 对于主面板(包含所有其他面板的面板-我将称之为主面板!)我在创建它时使用了以下代码: 对于其中的每个面板,我也使用BoxLayout,但是我已经在每个面板上尝试了[jpanel].setAlignmentX(Component.LEFT_ALIGNMENT)之类的东西,但这似乎不起作用。 任何帮助都将不胜感激!

  • 我有一个带有JLabel、JTextField的JPanel,还有一个带有JLabel的JPanel。 最后一个JLabel结果在某个操作后打印成功消息。 然而,问题是当我得到成功响应时,成功消息而不是在底部出现在第一个JLabel的右侧。我对秋千很陌生。有谁能帮帮我吗?

  • 我想将我的两个组件与窗口的左上角对齐。 我想要左上角有数字的线条。 请注意,我知道JFrame(“this”类)和主面板都使用了边框布局。 另一个不相关的问题:我应该什么时候创建一个新的对象?为什么我不能在私有实例变量中存储一个用于所有需要的使用?

  • 我想知道我们是否可以有一个布局不同于其父JFrame的JPanel。例如。如果我有JFrame与边框布局,我们有一个JPanel嵌入到它,它有不同的布局。有可能吗? 我正在努力做这件事。但是这样JPanel的组件就不会显示出来了。