Java Mid Layout (MidLayout) – Example สำหรับ MidLayout เป็นรูปแบบการจัดวาง Layout แบบ Table (ตาราง) โดยในขั้นแรกจะต้องประกาศจำนวน Column และ Rows จากนั้นการจัดวางตำแหน่งของ Component Controls จะอ้างถึง cell ต่าง ๆ เช่น add(btn1, “cell 3 1”); หรือ add(btn2, “cell 5 2”);

Java Mid Layout (MidLayout) – Example
Syntax
1.
getContentPane().setLayout(
new
MigLayout(
""
,
"[][][][][][][]"
,
"[][][][][][][]"
));
2.
3.
JButton btn1 =
new
JButton(
"Button 1"
);
4.
getContentPane().add(btn1,
"cell 3 1"
);
5.
6.
JButton btn2 =
new
JButton(
"Button 2"
);
7.
getContentPane().add(btn2,
"cell 5 2"
);
Component Control ของ Layout
เครื่องมือ Layout ที่อยู่บน GUI Controls
มุมมองเมื่อจัดการ Layout บน GUI Tools
Example
MyForm.java
01.
package
com.java.myapp;
02.
03.
import
java.awt.EventQueue;
04.
import
javax.swing.JFrame;
05.
import
javax.swing.JButton;
06.
import
net.miginfocom.swing.MigLayout;
07.
08.
public
class
MyForm
extends
JFrame {
09.
10.
/**
11.
* Launch the application.
12.
*/
13.
public
static
void
main(String[] args) {
14.
EventQueue.invokeLater(
new
Runnable() {
15.
public
void
run() {
16.
MyForm frame =
new
MyForm();
17.
frame.setVisible(
true
);
18.
}
19.
});
20.
}
21.
22.
/**
23.
* Create the frame.
24.
*/
25.
public
MyForm() {
26.
27.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28.
setBounds(
100
,
100
,
458
,
287
);
29.
setTitle(
"ThaiCreate.Com GUI Tutorial"
);
30.
31.
getContentPane().setLayout(
new
MigLayout(
""
,
"[][][][][][][]"
,
"[][][][][][][]"
));
32.
33.
JButton btn1 =
new
JButton(
"Button 1"
);
34.
getContentPane().add(btn1,
"cell 3 1"
);
35.
36.
JButton btn2 =
new
JButton(
"Button 2"
);
37.
getContentPane().add(btn2,
"cell 5 2"
);
38.
39.
JButton btn3 =
new
JButton(
"Button 3"
);
40.
getContentPane().add(btn3,
"cell 0 4"
);
41.
42.
JButton btn4 =
new
JButton(
"Button 4"
);
43.
getContentPane().add(btn4,
"cell 6 6"
);
44.
45.
}
46.
}
Output
แสดงการจัดวาง Layout แบบ MidLayout
แหล่งที่มา : thaicreate.com/java/java-gui-layout-midlayout