การสร้าง Frame โดยใช้ JFrame ใน Java
การสร้าง Frame โดยใช้ JFrame ใน Java
ตัวอย่างโค้ดนี้เป็นตัวอย่างการสร้าง Frame โดยใช้ JFrame ในภาษา Java ตัวอย่างโค้ดimport javax.swing.JFrame; public class JavaCodeExam { public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setVisible(true); frame.setSize(500, 200); frame.setLocation(10, 10); frame.setTitle("Frame By Java Code Exam"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }ตัวอย่างนี้เป็นตัวอย่างการสร้าง Frame โดยใช้ JFrame
ตัวอย่างการ set ค่าต่าง ๆ ให้กับ Frame
- setVisible เกี่ยวกับการแสดงผล (Shows or hides this Window depending on the value of parameter)
- setSize ใส่ขนาดให้กับ Frame (This method changes layout-related information, and therefore, invalidates the component hierarchy)
- setLocation ใส่ตำแหน่งของการแสดง (Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent)
- setTitle ใส่ค่า title ให้กับ Frame (Sets the title for this frame to the specified string)
- setDefaultCloseOperation เซตค่า default เมื่อกดปุ่มปิด (Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices)
