GridBagLayout和FlowLayout:出现在意外位置的组件

时间:2014-05-06 00:50:53

标签: java swing user-interface gridbaglayout flowlayout

1。关于GridBagLayout的问题: - 尽管为所有组件设置 gridx = 0; ,但它们都在父级的中心对齐。 请告诉我如何修复它,请告诉我为什么会发生这种意外行为。

2。关于FlowLayout的问题: -

  1. 在示例的开头,一个面板(添加到父级的顶部 使用GridBagLayout的面板正在使用FlowLayout。根据 构造函数FlowLayout(FlowLayout.LEFT, 5, 5)中的文档,FlowLayout.LEFT用于对齐,接下来的两个int是 适用于hgapvgap我正在使用左对齐,但仍然都是 按钮出现在中间。请告诉我原因?

  2. 所有组件的垂直插图远远超出预期。 为什么会这样?相同的5值不会导致双方插入如此多:s

  3. 示例代码: -

    Card(){
    
            setLayout(new GridBagLayout());
    
            JPanel panelBtns= new JPanel();
            panelBtns.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
            JButton emailBtn= new JButton("a");
            JButton saveBtn= new JButton("b");
            panelBtns.add(emailBtn);
            panelBtns.add(saveBtn);
            GridBagConstraints c= new GridBagConstraints();
            c.gridx=0;
            c.gridy=0;
            c.insets= new Insets(5,5,5,5);
            c.weightx=1;
            c.weighty=1;
            add(panelBtns, c);
    
            JLabel labelTitle= new JLabel("Title");
            labelTitle.setFont(new Font("calibri", Font.PLAIN, 12));
            c= new GridBagConstraints();
            c.gridx=0;
            c.gridy=1;
            c.insets=new Insets(5,5,5,5);
            c.weightx=0.35;
            c.weighty=1;
            add(labelTitle, c);
    
            JTextField textField= new JTextField();
            Border border = BorderFactory.createLineBorder(Color.GRAY);
            textField.setBorder(BorderFactory.createCompoundBorder(border, 
                        BorderFactory.createEmptyBorder(2,2,2,2)));
            c= new GridBagConstraints();
            c.gridx=0;
            c.gridy=2;
            c.insets= new Insets(5,5,5,5);
            c.fill=c.HORIZONTAL;
            c.weightx= 1;
            c.weighty=1;
            add(textField,c); 
    
            JLabel bodyLabel= new JLabel("Detail");
            bodyLabel.setFont(new Font("calibri", Font.PLAIN, 12));
            c= new GridBagConstraints();
            c.gridx=0;
            c.gridy=3;
            c.insets=new Insets(5,5,5,5);
            c.weightx=1;
            c.weighty=1;
            add(bodyLabel, c);
    
            JTextArea textArea= new JTextArea();
            Border border1 = BorderFactory.createLineBorder(Color.GRAY);
            textArea.setBorder(BorderFactory.createCompoundBorder(border1, 
                        BorderFactory.createEmptyBorder(2,2,2,2)));
            c= new GridBagConstraints();
            c.gridx=0;
            c.gridy=4;
            c.insets= new Insets(5, 5, 5,5);
            c.fill=c.BOTH;
            c.weightx=1;
            c.weighty=10;
            add(textArea,c);
    
        }
    

    enter image description here


    编辑: -

    我更改了插图的值,但是组件上方和下方的不需要的插图仍然存在 ...这里看起来几乎没问题,因为我使用了一个小的框架大小来截屏,但是当框架更大,看起来很丑陋和错位。

    我希望panelBtnstitleLabel之间的差距更小,而textFielftitleLabel之间的差距更小......同样适用于{{1} }和bodyLabel

    textArea

    enter image description here

1 个答案:

答案 0 :(得分:3)

  

关于GridBagLayout的问题: - 尽管设置gridx = 0;为了所有的   组件,所有组件都在父级的中心对齐。   请告诉我如何解决它,请告诉我为什么这样   意外行为发生。

这是GridBagLayout的默认行为。您需要使用GridBagConstrints#fill和/或GridBagConstrints#anchor(以及可能GridBagConstrints#weightx)的组合来更改组件占用给定单元格可用空间的方式

只需将c.anchor = GridBagConstraints.WEST;添加到bodyLayoutlabelTitle约束中......

Test

  

在示例的开头,一个面板(添加到父级的顶部   使用GridBagLayout的面板正在使用FlowLayout。根据   文档,在构造函数FlowLayout(FlowLayout.LEFT,5,5)中,   FlowLayout.LEFT用于对齐,接下来两个int用于hgap和   vgap。我正在使用左对齐但仍然出现两个按钮   中心。请告诉我为什么?

这是因为GridBagLayout正在放置按钮所在的JPanel位于布局的中心。

如果你添加panelBtns.setBorder(new LineBorder(Color.RED)),你可以实际测试并看到这个,你会看到panelBtns占用足够的空间以允许按钮可见(包括布局管理器中的填充)< / p>

Test

尝试将c.fill = GridBagConstaints.HORIZONTAL添加到按钮面板的约束

Test

<强>更新

  

我更改了insets的值,但是上面和下面是不需要的插入   组件仍在那里 ......这看起来几乎没问题,因为   我使用了一个小框架大小的屏幕截图,但框架是   更大,它看起来很丑陋和错位。

     

我希望panelBtnstitleLabel之间的差距更小,而且   textFielftitleLabel之间的差距更小......   同样适用于bodyLabeltextArea

从除weighty之外的所有内容中删除JTextArea约束。您应该知道weighty/x是从0到1的度量,它表示组件在其父级中应占用的可用空间百分比。

这些权重(大部分)均匀分布在没有足够空间容纳所有请求的地方。也就是说,如果你提供两个等于1的权重,它们将被赋予相等的数量(大约50%)。

Test

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class TestLayout1 {

    public static void main(String[] args) {
        new TestLayout1();
    }

    public TestLayout1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            JPanel panelBtns = new JPanel();
            panelBtns.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
            JButton emailBtn = new JButton("a");
            JButton saveBtn = new JButton("b");
            panelBtns.add(emailBtn);
            panelBtns.add(saveBtn);
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 0;
//            c.insets = new Insets(5, 5, 5, 5);
            c.insets= new Insets(2,2,0,2); // panelBtns
            c.weightx = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
//            c.weighty = 1;
            add(panelBtns, c);

            JLabel labelTitle = new JLabel("Title");
            labelTitle.setFont(new Font("calibri", Font.PLAIN, 12));
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 1;
//            c.insets = new Insets(5, 5, 5, 5);
            c.insets=new Insets(0,5,0,5); //titleLabel
            c.weightx = 0.35;
            c.anchor = GridBagConstraints.WEST;
//            c.weighty = 1;
            add(labelTitle, c);

            JTextField textField = new JTextField();
            Border border = BorderFactory.createLineBorder(Color.GRAY);
            textField.setBorder(BorderFactory.createCompoundBorder(border,
                    BorderFactory.createEmptyBorder(2, 2, 2, 2)));
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 2;
//            c.insets = new Insets(5, 5, 5, 5);
            c.insets= new Insets(0,5,0,5); //textField
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1;
//            c.weighty = 1;
            add(textField, c);

            JLabel bodyLabel = new JLabel("Detail");
            bodyLabel.setFont(new Font("calibri", Font.PLAIN, 12));
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 3;
            c.insets=new Insets(0,5,0,5); //bodyLabel
//            c.insets = new Insets(5, 5, 5, 5);
            c.anchor = GridBagConstraints.WEST;
            c.weightx = 1;
//            c.weighty = 1;
            add(bodyLabel, c);

            JTextArea textArea = new JTextArea();
            Border border1 = BorderFactory.createLineBorder(Color.GRAY);
            textArea.setBorder(BorderFactory.createCompoundBorder(border1,
                    BorderFactory.createEmptyBorder(2, 2, 2, 2)));
            c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = 4;
//            c.insets = new Insets(5, 5, 5, 5);
            c.insets= new Insets(0, 5, 5,5); //textArea
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1;
            c.weighty = 1;
            add(textArea, c);
        }
    }

}