OverlayLayout
- OverlayLayout เป็น subclass ของ Object จัดเรียง components ทับกันบนด้านบน และใช้ component alignments ที่ระบุเพื่อจัดตำแหน่ง relatively
- เมื่อกำหนดขนาดต่างกันให้ components ใดๆ เราจะเห็น components ทั้งหมด
- เพื่อจัดตำแหน่ง components ทับกันหรือที่ไหนก็ได้ใน frame ใช้สองเมธอด setAlignmentX() และ setAlignmentY() พารามิเตอร์เป็นค่า floating point 0.0f ถึง 1.0f OverlayLayout ใช้ค่า max 1.0f เป็น default
- เมธอดสำคัญของ OverlayLayout ได้แก่ addLayoutComponent(), getTarget(), invalidateLayout(), maximumLayoutSize() เป็นต้น
ตัวอย่าง
import java.awt.*;
import javax.swing.*;
import javax.swing.OverlayLayout;
public class OverlayLayoutTest extends JFrame {
public OverlayLayoutTest() {
setTitle("OverlayLayout Test");
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JButton button = new JButton("Small");
button.setMaximumSize(new Dimension(75, 50));
button.setBackground(Color.white);
panel.add(button);
button = new JButton("Medium Btn");
button.setMaximumSize(new Dimension(125, 75));
button.setBackground(Color.lightGray);
panel.add(button);
button = new JButton("Large Button");
button.setMaximumSize(new Dimension(200, 100));
button.setBackground(Color.orange);
panel.add(button);
add(panel, BorderLayout.CENTER);
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
new OverlayLayoutTest();
}
}
Output
