0

可能重复:
GridBagLayout 中的对齐问题

请看下面的代码

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

public class TestForm extends JFrame
{
    private JLabel firstLabel, secondLabel, thirdLabel, fourthLabel, fifthLabel;

    private JTextField firstTxt, secondTxt, thirdTxt, fourthTxt, fifthTxt;

    private JPanel centerPanel;
    private JPanel southPanel;
    private JLabel comboLabel;
    private JComboBox percentageCombo;
    private JLabel endTargetLabel;
    private JLabel mustLoseLabel;

    private GridBagLayout mainLayout = new GridBagLayout();
    private GridBagConstraints mainCons = new GridBagConstraints();

      public TestForm()
      {
        //Declaring instance variables  
        firstLabel = new JLabel("First: ");
        secondLabel = new JLabel("Second: ");
        thirdLabel = new JLabel("Third: ");
        fourthLabel = new JLabel("Fourth: ");
        fifthLabel = new JLabel("Fifth: ");        
        comboLabel = new JLabel("Select System Performance: ");

        firstTxt = new JTextField(7);
        secondTxt = new JTextField(7);
        thirdTxt = new JTextField(7);
        fourthTxt = new JTextField(7);
        fifthTxt = new JTextField(7);

        endTargetLabel = new JLabel("Your End Target Performance is: ");
        mustLoseLabel = new JLabel("Sammple Performance You Must Lose: ");  

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");


       this.setLayout(mainLayout);
        mainCons.gridy = 1;
        mainCons.gridx = 1;
        mainCons.anchor = GridBagConstraints.NORTH;
        this.add(createNorthPanel(),mainCons);

        mainCons.anchor = GridBagConstraints.WEST;
        mainCons.gridy = 2;
        mainCons.gridx = 1;
        mainCons.anchor = GridBagConstraints.CENTER;
        mainCons.insets = new Insets(15,0,0,0);
        this.add(createCenterPanel(),mainCons);

        mainCons.anchor = GridBagConstraints.SOUTH;
        mainCons.gridy = 3;
        mainCons.gridx = 1;
        this.add(createSouthPanel(),mainCons);

        this.setTitle("The Test Form");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createNorthPanel()
    {
        JPanel northPanel = new JPanel();

        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

        JLabel logoLabel = new JLabel();
        logoLabel.setIcon(new ImageIcon(getClass().getResource("/images/TESTING-LOGO.gif")));

        northPanel.add(logoLabel);

        return northPanel;
    }


    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(secondLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(secondTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(thirdLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(thirdTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(fourthLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(fourthTxt,gbc);

        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,7,0,0);
        centerPanel.add(fifthLabel,gbc);

        gbc.gridx = 6;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(fifthTxt,gbc);

        centerPanel.setBorder(BorderFactory.createTitledBorder("The Testing Form"));
        centerPanel.validate();

        return centerPanel;

    }


     private JPanel createSouthPanel()
    {
        southPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        southPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        southPanel.add(comboLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        southPanel.add(percentageCombo,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10,0,0,0);
        southPanel.add(endTargetLabel,gbc);


        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10,0,0,0);
        southPanel.add(mustLoseLabel,gbc);

        southPanel.setBorder(BorderFactory.createTitledBorder("See Your End Target "));

        return southPanel;
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestForm();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是我使用此代码时得到的

在此处输入图像描述

但是,我需要以下

  1. southPanel 和 centerPanel 都应该出现在一条垂直线上。在这里,southPanel 与 centerPanel 不在同一行,它更靠右对齐
  2. 两个面板都应该向左移动一点(如图所示)
  3. 两个面板应具有相同的大小
  4. 面板尺寸太小。它们应该比现在大一点,同时不改变其组件的空间和对齐方式。

在此处输入图像描述

如果您知道至少一个问题的答案,请提供帮助。我也附上徽标。

在此处输入图像描述

注意:从代码中删除更多元素不会产生原始问题。这就是为什么这段代码有点大。

4

2 回答 2

5

您需要为每个面板分配更多“权重”,这将迫使它们在给定空间内对齐。

在此处输入图像描述

this.setLayout(mainLayout);
mainCons.gridy = 1;
mainCons.gridx = 1;
mainCons.anchor = GridBagConstraints.NORTHWEST;
mainCons.weightx = 1;
this.add(createNorthPanel(), mainCons);

mainCons.anchor = GridBagConstraints.WEST;
mainCons.weightx = 1;
mainCons.gridy = 2;
mainCons.gridx = 1;
mainCons.insets = new Insets(15, 0, 0, 0);
this.add(createCenterPanel(), mainCons);

mainCons.anchor = GridBagConstraints.SOUTHWEST;
mainCons.weightx = 1;
mainCons.gridy = 3;
mainCons.gridx = 1;
this.add(createSouthPanel(), mainCons);

作为一个侧节点,您实际上可以使用相同的实例GridBagConstraints并仅更改您需要的那些值,这使您的代码更易于阅读(更不用说减少您需要输入的数量;))

您可能想看看如何使用 GridBagLayout

于 2013-01-08T08:50:36.823 回答
4

最简单的解决方案是BorderLayout为您的 JFrame 选择并添加您的 3 个面板,北、中和南,并带有约束BorderLayout.NORTH, BorderLayout.CENTERBorderLayout.SOUTH.

否则,您需要修改您的mainCons

mainCons.weightx = 1.0;
mainCons.anchor = GridBagConstraints.WEST;

anchor并且fill总是要求weightx和/或weighty设置为大于 0 的值。

如果您希望两个面板具有相同的“宽度”,您还可以设置

mainCons.fill = GridBagConstraints.HORIZONTAL;
于 2013-01-08T08:51:47.367 回答