有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

GridBagLayout中的java对齐问题

请看一下下面的代码

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();
        }

    }
}

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

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

enter image description here

正如您在这里看到的,southPanel中的元素与centerPanel没有很好地对齐。也就是说,我希望southPaneljlabel与centerPaneljlabel的起始位置相同。但结果是这样的。我希望southPanel中的jlabel出现在centerPanel的jlabel开始的同一行中。我所说的清楚地显示在下图中。我该怎么做?请帮忙

enter image description here

附言:我也附上了“测试图像”,所以你可以测试它,如果你想。 enter image description here


共 (1) 个答案

  1. # 1 楼答案

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

    您可以更改顶层布局,使组件左对齐(例如,在另一个GridBagLayout中使用西/左锚)

    旁注:这些负片会给你带来麻烦。 旁注2:不要手动设置gridx和gridy,而是查看REMAINDER 旁注3:大多数gbc是一致的。重复使用它们

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