与布局经理一起工作 [英] Working with layout managers

查看:304
本文介绍了与布局经理一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FlowLayout制作厨房展示系统,并且试图找到一种在第一行已满时在第二行添加另一个面板的方法. GUI的宽度将根据用户的喜好而改变.宽时,每行应显示更多组件.

I'm trying to make a kitchen display system using a FlowLayout and I'm trying to figure out a way to add another panel on the 2nd row when the first row is already full. The width of the GUI will change according to user preference. When wider, it should show more of the components per row.

推荐答案

方法-使用WrapLayout

的可变宽度

GridLayout解决方案假定GUI每行需要 6个组件.

Approach - Variable width with WrapLayout

The GridLayout solution presumes the GUI requires 6 components per row.

对于填充宽度所需的列数,&然后在所需的尽可能多的行中显示组件,请查看 WrapLayout .

For as many cols as needed to fill the width, & then show the components in as many rows as required, look to WrapLayout.

在这里也可以使用JList,因为似乎所有组件都由单个(GUI)对象组成.

A JList can also be used here, since it seems all the components consist of a single (GUI) object.

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

public class ListComponents {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String[] listData = {
                    "Component 1", "Component 2", "Component 3",};
                final DefaultListModel<String> model
                        = new DefaultListModel<String>();
                for (String datum : listData) {
                    model.addElement(datum);
                }
                JList list = new JList(model);
                list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
                list.setVisibleRowCount(-1);
                list.setCellRenderer(new ObjectCellRenderer());

                Action addAction = new AbstractAction("Add New") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        model.addElement("New Component");
                    }
                };
                JButton addNew = new JButton(addAction);

                JPanel ui = new JPanel(new BorderLayout(3, 3));
                ui.setBorder(new EmptyBorder(4, 4, 4, 4));
                ui.add(new JScrollPane(list), BorderLayout.CENTER);

                ui.add(addNew, BorderLayout.PAGE_START);

                JFrame f = new JFrame("Component List");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(ui);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }

}

class ObjectCellRenderer extends DefaultListCellRenderer {

    Border border = new EmptyBorder(20, 5, 20, 5);

    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = (JLabel) super.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        label.setBorder(border);
        return label;
    }
}

方法-使用GridLayout

的固定宽度

使用GridLayout时,我们总是希望每行固定的数字,而不管宽度如何.

Approach - Fixed width with GridLayout

Using a GridLayout, when we know we always want a fixed number per row, regardless of width.

JPanel mainPanel = new JPanel(new GridLayout(0,6));

请参见 new GridLayout(rows,cols) :

创建具有指定行数和列数的网格布局.布局中的所有组件都具有相等的大小.

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.

行和列中的一个(但不是全部)可以为零,,这意味着可以在行或列中放置任意数量的对象.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

有关使用GridLayout(0,2)表示添加其他标签下面的标签的示例,请参见此代码. kbd>

See this code for an example using a GridLayout(0,2) for the labels beneath Add Another Label

这篇关于与布局经理一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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