A JPanel là một vùng chứa và nó là một vô hình thành phần trong Java. FlowLayout là bố cục mặc định cho JPanel . Chúng tôi có thể thêm hầu hết các thành phần như nút , trường văn bản, nhãn, bảng, danh sách, cây và v.v. vào JPanel . Chúng tôi có thể đặt màu nền cho JPanel bằng cách sử dụng setBackground () phương pháp.
Ví dụ
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();
}
} Đầu ra