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 の両方が 1 つの垂直線に表示される必要があります。ここでは、southPanel は centerPanel と同じ行になく、少し右に配置されています。
  2. 両方のパネルが少し左に移動する必要があります (画像に表示)
  3. 両方のパネルが同じサイズである必要があります
  4. パネルのサイズが小さすぎます。コンポーネントのスペースと配置を変更しないで、現在よりも少し大きくする必要があります。

ここに画像の説明を入力

少なくとも1つの質問の答えを知っている場合は、助けてください. ロゴも貼ってます。

ここに画像の説明を入力

注: コードからさらに要素を削除しても、元の問題は発生しません。そのため、このコードは少し大きいです。

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

最も簡単な解決策は、JFrame を選択BorderLayoutし、北、中央、南の 3 つのパネルを制約BorderLayout.NORTH, BorderLayout.CENTERとで追加することBorderLayout.SOUTHです。

それ以外の場合は、以下を変更する必要がありますmainCons:

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

anchorfill常にそれを要求し、およびweightx/またはweighty0 より大きい値に設定します。

両方のパネルの「幅」を同じにしたい場合は、設定することもできます

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