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

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

问题描述

当你不知道你将拥有多少组件以及它们有多大时,你会如何获得一个体面的gui?



用户例如,输入他们想要的文本字段数以及哪些文本字段在边界面板中分组,程序生成它。



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



我希望所有组件都具有最小尺寸,但是,您知道,仍然可用。



< a href =http://grab.by/5giq =noreferrer>现在的示例,使用GridLayout ,所有字段都是正常的,单行JTextFields,其中标题为date的面板完全是挤压(它有三个字段),第一级字段是大的。任何人都有任何指针?

解决方案

MiGLayout有很多吸引力,但

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

公共类BoxTest扩展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));
}

私有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(内部);
}
返回外部;
}

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();
}
});
}
}


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天全站免登陆