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

 

效果如下:
jgoodies FormLayout Demo

  

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

jgoodies FormLayout Demo

 

下面的代码,效果和上面完全一致,用的是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);
    }
}
 

 

 

 

你可能感兴趣的:(layout)