JGoodies FormLayout

来源:互联网 发布:计算字数的软件 编辑:程序博客网 时间:2024/05/18 02:44

官网:http://www.jgoodies.com/freeware/libraries/forms/ 

下载地址:http://www.jgoodies.com/downloads/libraries/

需要jgoodies-forms-1.7.1.jar和jgoodies-common-1.6.0.jar

进一步研究,请阅读JGoodies Forms的白皮书、api文档、demo及源码!


转载请标明出处!




package com.gavin.demo;


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


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


/**
 * @author gavin
 */
public class Example1 extends JFrame{

public Example1(){
initUI();
}




private void initUI() {
/**
pref : preferred size
min : minimum size
dlu:  字体大小的单位
px:pixels 像素
 
the panel’s grid – including the columns and rows for the gaps between the components.
panel中包含列、行、间隔,即row、column、gap
以下例子是一个5行5列(包含间隔)的界面,但是看到的数据是三行三列:
————————————————————————————————————————————————————
Label1   gap    textField1     textField1             textField1
————————————————————————————————————————————————————
>>>>>>>>>>>>>>>>>>>>>gap<<<<<<<<<<<<<<<<<<<<<<<<
————————————————————————————————————————————————————
Label2 gap    extField2     gap no data
————————————————————————————————————————————————————
>>>>>>>>>>>>>>>>>>>>>gap<<<<<<<<<<<<<<<<<<<<<<<<
————————————————————————————————————————————————————
Label3 gap   textField3  gap          detailsButton
————————————————————————————————————————————————————
CellConstraints构造器的六个参数:
grid x(col), grid y(row), grid width (column span), grid height (row span), horizontal alignment and vertical alignment.
 
FormLayout构造器:
eg1:
 // Label, gap, component
FormLayout layout = new FormLayout(
     "pref, 4dlu, pref");

// Right-aligned label, gap, component, gap, component
FormLayout layout = new FormLayout(
     "right:pref, 4dlu, 50dlu, 4dlu, 50dlu");

// Left-aligned labels, gap, components, gap, components
FormLayout layout = new FormLayout(
     "left:pref, 4dlu, pref, 4dlu, pref");
     
eg2:

(说明:The columns and rows are specified by three parts: a mandatory size, an
optional default alignment and an optional resizing behavior.
即:pref-->一个固定的尺寸
left/right-->可选的对齐方式
grow-->可选的调整大小的行为)  
 
new FormLayout(“right:pref, 10px, left:pref:grow”, // 3 columns
“pref, 4px, pref, pref:grow”); // 4 rows

ColumnSpec supports the following alignments: left, center, right, fill;
RowSpec supports top, center, bottom, fill.
 
>>>>Spec 1: Column and Row String Encoding Syntax <<<<<<
columnSpec ::= [columnAlignment:] size [:resizeBehavior]
rowSpec ::= [rowAlignment :] size [:resizeBehavior]
columnAlignment ::= LEFT | CENTER | RIGHT | FILL | L | C | R | F
rowAlignment ::= TOP | CENTER | BOTTOM | FILL | T | C | B | F
size ::= constantSize | componentSize | boundedSize
componentSize ::= MIN | PREF | DEFAULT | M | P | D
constantSize ::= <integer>integerUnit | <double>doubleUnit
integerUnit ::= PX | PT | DLU
doubleUnit ::= IN | MM | CM
boundedSize ::= MIN(constantSize;componentSize)
| MAX(constantSize;componentSize)
resizeBehavior ::= NONE | GROW | GROW(<double>) | G(<double>)
>>>>Spec 1: Column and Row String Encoding Syntax  <<<<<<

eg:
“10px”, “4dlu”, “min”, “pref”, “default”, “left:6px”, “right:6dlu”,
“left:pref:grow”, “pref:grow(0.5)”, “l:m:g(0.8)”, “left:max
(50dlu;pref)”


layout.setColumnGroups(new int[][]{ {2, 4} });
layout.setRowGroups (new int[][]{ {1, 4}, {2, 3} });
说明:Column and row groups specifiy that a set of columns or rows will get the
same width or height.

>>>>>>>>>>>>>>>>Spec 2: Cell Constraints String Encoding Syntax<<<<<<<<<<<<<<<<<<
constraints ::= column, row [, colSpan, rowSpan][, hAlign, vAlign]
column ::= <integer>
row ::= <integer>
colSpan ::= <integer>
rowSpan ::= <integer>
hAlign ::= LEFT | CENTER | RIGHT | DEFAULT | FILL
| L | C | R | D | F
vAlign ::= TOP | CENTER | BOTTOM | DEFAULT | FILL
| T | C | B | D | F
>>>>>>>>>>>>>>>>Spec 2: Cell Constraints String Encoding Syntax<<<<<<<<<<<<<<<<<<



*/
FormLayout layout = new FormLayout(
"pref, 4dlu, 50dlu, 4dlu, min", // columns:各列的大小
"pref, 2dlu, pref, 2dlu, pref"); // rows:各行的大小

layout.setRowGroups(new int[][]{{1, 3, 5}});//第1,3,5行具有相同的高度
JPanel panel = new JPanel();
panel.setLayout(layout);
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JTextField textField3 = new JTextField();
JButton detailsButton = new JButton("detail");
CellConstraints cc = new CellConstraints();//网格约束
panel.add(new JLabel("Label1"), cc.xy (1, 1));  //第一列,第一行
panel.add(textField1, cc.xyw(3, 1, 3));//第一列,第三行,占3列的宽度
panel.add(new JLabel("Label1"), cc.xy (1, 3));//第一列,第三行
panel.add(textField2, cc.xy (3, 3));//第3列,第3行
panel.add(new JLabel("Label3"), cc.xy (1, 5));//第一列,第五行
panel.add(textField3, cc.xy (3, 5));//第3列,第五行
panel.add(detailsButton, cc.xy (5, 5));//第5列,第5行

this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
setContentPane(panel);
}




public static void main(String[] args) {
JFrame jf = new Example1();
jf.pack();
jf.setLocationRelativeTo(null); //居中
jf.setVisible(true);
}


}




package com.gavin.demo;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


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


/**

 * PanelBuilder 
 * @author gavin
 */
public class Example2 extends JFrame{

public Example2(){
initUI();
}

private void initUI() {

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

JTextField idField = new JTextField();
JTextField powerField = new JTextField();
JTextField ptiField = new JTextField();
JTextField lenField = new JTextField();
JTextField daField = new JTextField();
JTextField diField = new JTextField();
JTextField da2Field = new JTextField();
JTextField di2Field = new JTextField();
JTextField rField = new JTextField();
JTextField dField = new JTextField();
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(idField, cc.xy (3, 3));
builder.addLabel("PTI [kW]", cc.xy (1, 5));
builder.add(ptiField, cc.xy (3, 5));
builder.addLabel("Power [kW]", cc.xy (5, 5));
builder.add(powerField, cc.xy (7, 5));
builder.addLabel("len [mm]", cc.xy (1, 7));
builder.add(lenField, cc.xy (3, 7));
builder.addSeparator("Diameters", cc.xyw(1, 9, 7));
builder.addLabel("da [mm]", cc.xy (1, 11));
builder.add(daField, cc.xy (3, 11));
builder.addLabel("di [mm]", cc.xy (5, 11));
builder.add(diField, cc.xy (7, 11));
builder.addLabel("da2 [mm]", cc.xy (1, 13));
builder.add(da2Field, cc.xy (3, 13));
builder.addLabel("di2 [mm]", cc.xy (5, 13));
builder.add(di2Field, cc.xy (7, 13));
builder.addLabel("R [mm]", cc.xy (1, 15));
builder.add(rField, cc.xy (3, 15));
builder.addLabel("D [mm]", cc.xy (5, 15));
builder.add(dField, cc.xy (7, 15));

JPanel panel = builder.getPanel();
panel.setLayout(layout);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
setContentPane(panel);
}

public static void main(String[] args) {
JFrame jf = new Example2();
jf.pack();
jf.setLocationRelativeTo(null); //居中
jf.setVisible(true);
}

}





package com.gavin.demo;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


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


/**

 * DefaultFormBuilder 
 * @author gavin
 */
public class Example3 extends JFrame{

public Example3(){
initUI();
}

private void initUI() {
JPanel panel = new JPanel();
JTextField idField = new JTextField();
JTextField powerField = new JTextField();
JTextField ptiField = new JTextField();
JTextField lenField = new JTextField();
JTextField daField = new JTextField();
JTextField diField = new JTextField();
JTextField da2Field = new JTextField();
JTextField di2Field = new JTextField();
JTextField rField = new JTextField();
JTextField dField = new JTextField();

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,panel);
builder.setDefaultDialogBorder();
builder.appendSeparator("Segment");
builder.append("Identifier", idField);
builder.nextLine();
builder.append("PTI [kW]", ptiField);
builder.append("Power [kW]", powerField);
builder.append("len [mm]", lenField);
builder.nextLine();
builder.appendSeparator("Diameters");
builder.append("da [mm]", daField);
builder.append("di [mm]", diField);
builder.append("da2 [mm]", da2Field);
builder.append("di2 [mm]", di2Field);
builder.append("R [mm]", rField);
builder.append("D [mm]", dField);

setDefaultCloseOperation(EXIT_ON_CLOSE);  
setContentPane(panel);
}

public static void main(String[] args) {
JFrame jf = new Example3();
jf.pack();
jf.setLocationRelativeTo(null); //居中
jf.setVisible(true);
}

}

原创粉丝点击