Swing 中的可变布局 [英] Variable Layout in Swing

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

问题描述

如果您不知道将拥有多少个组件以及它们有多大,您将如何生成外观漂亮的 gui?

例如,用户输入他们想要的文本字段的数量以及这些文本字段中的哪些被分组到带边框的面板中,然后程序会生成此内容.

我一直在使用 GridLayout,但问题是它使所有单元格具有相同的宽度和高度,当所有组件具有相同的大小时这很好,但是例如,当我有一个文本字段和一个边框时具有多个字段的面板,文本字段被拉伸或面板被挤压.

我希望所有组件都具有最小尺寸,但仍然可用.

import java.awt.*;导入 java.util.List;导入 java.util.ArrayList;导入 javax.swing.*;公共类 BoxTest 扩展 JPanel {私人列表字段 = 新 ArrayList();公共 BoxTest() {this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));this.add(createPane(3, "One ", Color.red));this.add(createPane(3, "Two", Color.green));this.add(createPane(10, "三", Color.blue));}私人 JPanel createPane(int n, String s, Color c) {JPanel 外 = 新 JPanel();外部.setLayout(新的BoxLayout(外部,BoxLayout.Y_AXIS));外部.setBorder(BorderFactory.createLineBorder(c, 2));for (int i = 0; i < n; i++) {JPanel 内 = 新 JPanel();inner.setLayout(new BoxLayout(inner, BoxLayout.X_AXIS));JLabel label = new JLabel(s + i + ":", JLabel.RIGHT);label.setPreferredSize(new Dimension(80, 32));内部添加(标签);JTextField tf = new JTextField("Stackoverflow!", 32);内加(tf);字段.添加(tf);外加(内);}返回外部;}私人无效显示(){JFrame f = new JFrame();f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JScrollPane jsp = new JScrollPane(this,JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_​​NEVER);this.validate();维度 d = this.getPreferredSize();d.高度/= 2;jsp.getViewport().setPreferredSize(d);jsp.getVerticalScrollBar().setUnitIncrement(this.getPreferredSize().height/fields.size());f.add(jsp);f.pack();f.setVisible(true);}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){新的 BoxTest().display();}});}}

How would you go about getting a decent looking gui generated when you don't know how many components you will have and how big they will be?

A user, for instance, enters how many textfields they want and which of those textfields are grouped in bordered panels and the program generates this.

I've been using GridLayout, but the problem is that it makes all cells of the same width and height, which is fine when all components have the same size, but when I, for example, have a textfield and a bordered panel with multiple fields, either the textfield gets stretched out or the panel is squeezed.

I would like all components to have their minimum size, but still, you know, usable.

Example of how it is now, using GridLayout, all fields are normal, one-line JTextFields where the panel titled date is totally squeezed (it has three fields in it) and the first level fields are way to big. Anyone have any pointers?

解决方案

MiGLayout has a lot of appeal, but BoxLayout is an alternative.

import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;

public class BoxTest extends JPanel {

    private List<JTextField> fields = new ArrayList<JTextField>();

    public BoxTest() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(createPane(3, "One ", Color.red));
        this.add(createPane(3, "Two ", Color.green));
        this.add(createPane(10, "Three ", Color.blue));
    }

    private JPanel createPane(int n, String s, Color c) {
        JPanel outer = new JPanel();
        outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS));
        outer.setBorder(BorderFactory.createLineBorder(c, 2));
        for (int i = 0; i < n; i++) {
            JPanel inner = new JPanel();
            inner.setLayout(new BoxLayout(inner, BoxLayout.X_AXIS));
            JLabel label = new JLabel(s + i + ":", JLabel.RIGHT);
            label.setPreferredSize(new Dimension(80, 32));
            inner.add(label);
            JTextField tf = new JTextField("Stackoverflow!", 32);
            inner.add(tf);
            fields.add(tf);
            outer.add(inner);
        }
        return outer;
    }

    private void display() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane jsp = new JScrollPane(this,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.validate();
        Dimension d = this.getPreferredSize();
        d.height /= 2;
        jsp.getViewport().setPreferredSize(d);
        jsp.getVerticalScrollBar().setUnitIncrement(
            this.getPreferredSize().height / fields.size());
        f.add(jsp);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new BoxTest().display();
            }
        });
    }
}

这篇关于Swing 中的可变布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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