jgoodies FormLayout Demo

以前看过一点jgoodies的布局,没有深入研究。这几天画界面,越看越难看,看不下去了,就要整布局。

 

在网上找资料,中文资料几乎没有。

 

翻*墙(这2个字连在一起,居然不能发,于是加了个*)到官网找,发现那里的Demo很好看,都是没有附源码的,狂晕。。。

 

这里写几个官方白皮书里面的例子,及对应的效果:

 

FormLayout:

 

package com.lippeng.helloworld;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class HelloWorld {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("HelloWorld");

        FormLayout layout = new FormLayout("pref, 4dlu, 50dlu, 4dlu, min", // columns
                "pref, 2dlu, pref, 2dlu, pref");// rows
        layout.setRowGroups(new int[][] { { 1, 3, 5 } });

        Container contentPane = jFrame.getContentPane();
        contentPane.setLayout(layout);

        CellConstraints cc = new CellConstraints();
        contentPane.add(new JLabel("Label1"), cc.xy(1, 1));
        contentPane.add(new JTextField(), cc.xyw(3, 1, 3));
        contentPane.add(new JLabel("Label2"), cc.xy(1, 3));
        contentPane.add(new JTextField(), cc.xy(3, 3));
        contentPane.add(new JLabel("Label3"), cc.xy(1, 5));
        contentPane.add(new JTextField(), cc.xy(3, 5));
        contentPane.add(new JTextField(), cc.xy(5, 5));

        jFrame.setVisible(true);
        jFrame.setSize(200, 200);
    }
}

 

效果如下:

  

DefaultFormBuilder :

 

package com.lippeng.helloworld;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class HelloWorld {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("HelloWorld");

        FormLayout layout = new FormLayout(//
                "right:max(40dlu;p), 4dlu, 80dlu, 7dlu, " // 1st major column
                        + "right:max(40dlu;p), 4dlu, 80dlu", // 2nd major column
                ""); // add rows dynamically

        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.setDefaultDialogBorder();

        builder.appendSeparator("Segment");
        builder.append("Identifier", new JTextField());
        builder.nextLine();
        builder.append("PTI [kW]", new JTextField());
        builder.append("Power [kW]", new JTextField());
        builder.append("len [mm]", new JTextField());
        builder.nextLine();
        builder.appendSeparator("Diameters");
        builder.append("da [mm]", new JTextField());
        builder.append("di [mm]", new JTextField());
        builder.append("da2 [mm]", new JTextField());
        builder.append("di2 [mm]", new JTextField());
        builder.append("R [mm]", new JTextField());
        builder.append("D [mm]", new JTextField());

        Container contentPane = jFrame.getContentPane();
        contentPane.setLayout(new BorderLayout());

        contentPane.add(builder.getPanel(), BorderLayout.CENTER);

        jFrame.setVisible(true);
        jFrame.setSize(600, 400);
    }
}
  

 

下面的代码,效果和上面完全一致,用的是PanelBuilder :

 

package com.lippeng.helloworld;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class HelloWorld {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("HelloWorld");

        FormLayout layout = new FormLayout("right:max(50dlu;p), 4dlu, 75dlu, 7dlu, right:p, 4dlu, 75dlu", "p, 2dlu, p, 3dlu, p, 3dlu, p, 7dlu, " + "p, 2dlu, p, 3dlu, p, 3dlu, p");
        PanelBuilder builder = new PanelBuilder(layout);
        builder.setDefaultDialogBorder();
        CellConstraints cc = new CellConstraints();
        builder.addSeparator("Segment", cc.xyw(1, 1, 7));
        builder.addLabel("Identifier", cc.xy(1, 3));
        builder.add(new JTextField(), cc.xy(3, 3));
        builder.addLabel("PTI [kW]", cc.xy(1, 5));
        builder.add(new JTextField(), cc.xy(3, 5));
        builder.addLabel("Power [kW]", cc.xy(5, 5));
        builder.add(new JTextField(), cc.xy(7, 5));
        builder.addLabel("len [mm]", cc.xy(1, 7));
        builder.add(new JTextField(), cc.xy(3, 7));
        builder.addSeparator("Diameters", cc.xyw(1, 9, 7));
        builder.addLabel("da [mm]", cc.xy(1, 11));
        builder.add(new JTextField(), cc.xy(3, 11));
        builder.addLabel("di [mm]", cc.xy(5, 11));
        builder.add(new JTextField(), cc.xy(7, 11));
        builder.addLabel("da2 [mm]", cc.xy(1, 13));
        builder.add(new JTextField(), cc.xy(3, 13));
        builder.addLabel("di2 [mm]", cc.xy(5, 13));
        builder.add(new JTextField(), cc.xy(7, 13));
        builder.addLabel("R [mm]", cc.xy(1, 15));
        builder.add(new JTextField(), cc.xy(3, 15));
        builder.addLabel("D [mm]", cc.xy(5, 15));
        builder.add(new JTextField(), cc.xy(7, 15));

        Container contentPane = jFrame.getContentPane();
        contentPane.setLayout(new BorderLayout());

        contentPane.add(builder.getPanel(), BorderLayout.CENTER);

        jFrame.setVisible(true);
        jFrame.setSize(600, 400);
    }
}
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值