布局管理器,用于垂直和水平排列可变大小的组件 [英] Layout Manager for lining up variably-sized components vertically and horizontally

查看:159
本文介绍了布局管理器,用于垂直和水平排列可变大小的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个相当基本的面板来添加/编辑数据库中的用户.为了做到这一点,我想在左侧的不同字段中具有多个标签,在右侧的字段中具有适当的控件. (因此,左侧可能会显示诸如用户名",用户类型",到期日期"之类的字样,并且相关联的右侧将具有文本面板,下拉菜单和日历小部件.)

我遇到的问题是我希望它们既水平排列(一个标签及其相关的控制区域"应从同一行开始),又垂直排列(所有标签应沿同一垂直线开始) ,并且所有控制区域均应在同一垂直线上开始.)不过,有些控制区域要比其他控制区域大或小.许多只包含一个文本面板,而其他一些则具有单选按钮,日历小部件等相关联的内容.

此外,所需字段的类型经常更改,并且取决于上下文(例如是创建新用户还是编辑旧用户,以及控制用户的权限级别),其中某些字段可能是排除在外.因此,我希望可以在很大程度上自行安排一些事情-如果我必须非常手动地设置位置,我希望事情会因为零件被排除而破裂.

我想看到的是一个布局管理器,它基本上可以让我在灵活的网格上设置坐标,然后自动调整网格大小以为每个元素提供最小的空间,同时仍保持网格线笔直.这样,我可以简单地保留控件及其相关标签的列表,并以编程方式构建面板,以便始终将标签/控件分配给相同的行.

尽管如此,我还没有弄清楚如何使用任何布局管理器来做到这一点:GridLayout似乎要求所有内容都具有相同的大小,并且BoxLayout想要垂直或水平排列,但不能同时排列.因此,我一直发现我的行排成一行,但没有垂直整齐地对齐,或者错误的标签与错误的控件排成一行.

有什么建议吗?

解决方案

下面是一个示例,如您所见,有2个正常大小的行,1个中等大小的行和1个较大(更小)的行,其顺序不同,我认为这是与您想做的事情类似

行的高度由GridBagConstraintsipady属性确定,您可以在

我希望您现在有了一个主意,如果您仍然有疑问,请问另一个(更具体的)问题,包括您的 mcve

I'm trying to make a fairly basic panel for adding/editing users in a database. To make this happen, I want to have a number of labels for different fields on the left, and the appropriate controls for that field on the right. (So, the left might say something like "Username", "User Type", "Expiration Date", etc, and the associated right-hand side would have a text panel, a drop down, and a calendar widget.)

The problem I'm running into is that I want these to both line up horizontally (a label and it's associated "control areas" should start at the same line), and vertically (all labels should start along the same vertical line, and all control areas should start on the same vertical line.) Some control areas are larger or smaller than others, though; many consist of only a single text panel, while others have things like radio buttons, calendar widgets, etc, associated with them.

Furthermore, the types of fields needed are changing frequently, and depending on context (such as whether a new user is being created or an old one edited, and the permissions level of the controlling user), some of these fields may be excluded. So, I want something that will largely arrange itself - if I have to set positions very manually, I expect things to break as parts are excluded.

What I'd like to see is a layout manager that will basically let me set coordinates on a flexible grid, and then size the grid automatically to provide minimum space to each element, while still keeping the grid lines straight. That way I could simply keep a list of controls and their associated labels, and build the panel programmatically so that labels/controls are always assigned to the same rows.

I haven't figured out how to do this with any of the layout managers though: GridLayout seems to require that everything be the same size, and BoxLayout wants to arrange things either vertically or horizontally but not both. So, I keep finding that either my rows line up but are not aligned neatly vertically, or the wrong labels line up with the wrong controls.

Any advice?

解决方案

Here's an example, as you can see, there are 2 normal sized rows, 1 medium sized and 1 larger (taller) row in different order, I think this is something similar to what you wanted to do

The height of the row is determined by the ipady attribute of the GridBagConstraints, you can read more on How to use GridBagLayout and the docs

I haven't used GridBagLayout a lot, so there might be some things that could be skipped or improved, but for a matter of having this example, here's the output of the code below:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridBagLayoutExample {
    private JPanel contentPane;
    private JFrame frame;
    private JLabel label;
    private JTextField field;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutExample().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("GridBagLayout Example");
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        label = new JLabel("A small label");
        field = new JTextField();

        c.insets = new Insets(0, 10, 0, 10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 0;
        contentPane.add(label, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 2;
        contentPane.add(field, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 1;
        c.ipady = 20;
        contentPane.add(new JLabel("Another but much larger JLabel"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 1;
        c.ipady = 20;
        c.gridwidth = 2;
        contentPane.add(new JButton("And including a larger JButton"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 2;
        c.ipady = 60;
        contentPane.add(new JLabel("A TALLER ROW!"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        c.ipady = 60;
        c.gridwidth = 2;
        contentPane.add(new JTextField(), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 3;
        c.ipady = 0;
        contentPane.add(new JLabel("Another normal row"), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 3;
        c.gridwidth = 2;
        contentPane.add(new JTextField(), c);

        frame.add(contentPane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I hope you have an idea with this in mind now, if you still have doubts, ask another (more specific) question including your mcve

这篇关于布局管理器,用于垂直和水平排列可变大小的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆