【问题标题】:Alignment issue in GridBagLayoutGridBagLayout 中的对齐问题
【发布时间】:2013-01-07 19:10:02
【问题描述】:

请看下面的代码

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

public class TestForm extends JFrame
{
    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,bfPercentageLabel;

    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;

    private JPanel centerPanel;
    private JPanel southPanel;
    private JLabel endTargetWeightLabel;
    private JLabel endTargetWeightResultLabel;
    private JLabel fatMustLoseLabel;
    private JLabel fatMustLoseResultLabel;


      public TestForm()
      {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");        
        bfPercentageLabel = new JLabel("The Orginal Test Score Is: ");

        heightTxt = new JTextField(7);
        weightTxt = new JTextField(7);
        waistTxt = new JTextField(7);
        neckTxt = new JTextField(7);
        hipsTxt = new JTextField(7);

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


        endTargetWeightResultLabel = new JLabel("d");
        fatMustLoseResultLabel = new JLabel("e");

        this.add(createNorthPanel(),"North");
        this.add(createCenterPanel(),"Center");
        this.add(createSouthPanel(),"South");
        this.add(new JPanel(),"West");
        this.add(new JPanel(),"East");
        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(heightLabel,gbc);

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

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

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

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

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

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

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

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

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



        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(50,0,0,0);
        gbc.gridwidth = 6;
        centerPanel.add(bfPercentageLabel,gbc);


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

        centerPanel.setPreferredSize(centerPanel.getPreferredSize());
        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(10,0,0,0);
        southPanel.add(endTargetWeightLabel,gbc);

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

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

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

        southPanel.setPreferredSize(new Dimension(centerPanel.getWidth(),100));
        southPanel.setBorder(BorderFactory.createTitledBorder("See Your End Target Weight"));

        return southPanel;
    }

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

    }
}

以上是可能的最大缩短代码。删除更多元素不会显示原始错误。

好的。现在的问题是关于对齐。运行代码后,您将看到以下内容。

正如您在那里看到的,southPanel 中的元素与centerPanel 没有很好地对齐。这意味着,我希望 southPanel JLabels 进入 centerPanel JLabels 开始的同一行。但它最终以这种方式结束。我希望 southPanel 中的 JLabels 出现在 centerPanel 的 JLabels 开始的同一行中。我所说的清楚地显示在下图中。我该怎么做?请帮忙!

PS:我也附上了“测试图像”,因此您可以根据需要进行测试。

【问题讨论】:

  • 您是否使用任何 IDE,例如 eclipse OR Netbeans。在那里,您可以打开表单视图并通过拖放来安排对齐方式。
  • @smit:不。Java 社区不接受 Java 中的拖放操作。而且,表单视图只有在您在可用的项目中编码时才可用
  • drag and drop 只是表达我的意思是如果你用 Windowbuilder Editor 打开你的文件,你可以安排你的字段
  • @smit:我知道。你说我可以拖放按钮、文本字段等。这是一种非常糟糕的做法。在 .NET 中,它被接受是因为它们似乎默认遵循 MVC。但在 Java 中,情况并非如此。我从包括国际 AI 研究人员(我的讲师)和关键系统开发人员(另一位讲师)以及我个人认识的在 Oracle 论坛担任职位的人那里接受了有关拖放的 cmet。据他们说,拖放文化正在浪费整个 Java。我同意,因为我已经看到人们用拖放操作做了什么
  • 我认为你没有明白我在说什么。我说过一旦你创建了你的表单,你可以在 Windowbuilder 中看到它的样子,在那里你可以改变你的对齐方式。我不是在谈论通过拖放创建表单。您可以通过任何方式向我展示您正在谈论的讲座的链接。

标签: java swing jpanel jlabel gridbaglayout


【解决方案1】:

由于每个面板都是水平居中的,除非它们具有相同的宽度,否则它们不会对齐。将它们放在一个公共面板中并共享GridBagLayout 会更容易。

您可以将顶层布局更改为左对齐组件(例如,在另一个带有 WEST/LEFT 锚点的 GridBagLayout 中)。

旁注:那些负面的插图会给你带来麻烦。 旁注2:不要手动设置gridx和gridy,而是查看REMAINDER 旁注3:您的大多数gbc都是相同的。重复使用它们。

this.setLayout(new GridBagLayout());
this.add(createLeftPanel(), gbc);
...

【讨论】:

  • 您好,您的回答可能就是解决方案。但由于它提供的信息较少,所以我不明白你在说什么。您介意扩展代码 sn-p 或编辑我的代码吗?
  • 好的,正如您所建议的,假设我要将所有这些添加到一个 GridBagLayout 中。但是,我有 2 个 JPanel,我需要每个 JPanel 中的“标题”。那我该怎么做呢?
  • 我想这就是答案,我做了一些更改,但我认为我也犯了一些错误,所以问题就在那里。但是,我遇到的其他一些问题已经消失了
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 2021-06-21
  • 2015-04-05
  • 1970-01-01
  • 2014-06-04
  • 2018-01-13
  • 1970-01-01
  • 2013-01-23
相关资源
最近更新 更多