A JPanel เป็นคอนเทนเนอร์และเป็น ล่องหน ส่วนประกอบ ในชวา FlowLayout เป็นเลย์เอาต์เริ่มต้นสำหรับ JPanel . เราสามารถเพิ่มส่วนประกอบส่วนใหญ่ได้ เช่น ปุ่ม , ช่องข้อความ ป้ายกำกับ ตาราง รายการ ต้นไม้ และอื่นๆ ไปยัง JPanel . เราสามารถตั้งค่าสีพื้นหลังเป็น JPanel โดยใช้ setBackground() วิธีการ
ตัวอย่าง
import java.awt.*
import javax.swing.*;
public class JPanelBackgroundColorTest extends JFrame {
private JPanel panel;
public JPanelBackgroundColorTest() {
setTitle("JPanelBackgroundColor Test");
panel = new JPanel();
panel.add(new JLabel("Welcome to Tutorials Point"));
panel.setBackground(Color.green);
add(panel, BorderLayout.CENTER);
setSize(375, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
new JPanelBackgroundColorTest();
}
} ผลลัพธ์
