Java Absolute Layout (AbsoluteLayout) - Example |
Java Absolute Layout (AbsoluteLayout) - Example สำหรับ AbsoluteLayout เป็นการจัดวาง Layout ในรูปแบบของ X,Y โดยอ้างตำแหน่งของ Controls จากตำแหน่งต่าง ๆ ที่อยู่ใน Frame จากแกน X และแกน Y ซึ่งทุก ๆ Controls ที่สร้างขึ้นจะต้องมีตำแหน่ง X,Y เป็นตัวระบุตำแหน่งด้วย และสามารถจัดวางได้อย่างอีสระ ซึ่ง Layout นี้จะได้รับความนิยมมากที่สุด เพราะสามารถใช้งานได้ง่าย
Java Absolute Layout (AbsoluteLayout) - Example
Syntax
getContentPane().setLayout(null); // ค่าของ AbsoluteLayout จะใช้ null
JButton btn1 = new JButton("Button 1");
btn1.setBounds(41, 29, 89, 23); // (x,y,height,width)
Component Control ของ Layout

Example
MyForm.java
package com.java.myapp;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyForm extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setTitle("ThaiCreate.Com GUI Tutorial");
getContentPane().setLayout(null);
JButton btn1 = new JButton("Button 1");
btn1.setBounds(41, 29, 89, 23);
getContentPane().add(btn1);
JButton btn2 = new JButton("Button 2");
btn2.setBounds(206, 50, 89, 23);
getContentPane().add(btn2);
JButton btn3 = new JButton("Button 3");
btn3.setBounds(53, 137, 89, 23);
getContentPane().add(btn3);
JButton btn4 = new JButton("Button 4");
btn4.setBounds(254, 94, 89, 23);
getContentPane().add(btn4);
}
}
Output

ตัวอย่าง Layout ของ AbsoluteLayout
Property & Method (Others Related) |
|