Rabu, 13 Agustus 2025

JAVA - JPanel

 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tjpanel;
import javax.swing.*;
import java.awt.*;

public class TJPanel extends JApplet {
    Container container = null;
    
    public void init() {
        // 1. Get a reference to the applet's content pane and 
        // assigns a grid layout.
        container = getContentPane();
        container.setLayout(new GridLayout(2,1,2,2));
     
        // 2. Create four panels with grid layout and specified number
        // of rows and columns. The first two numbers in the 
        // GridLayout constructors are the number of rows and columns
        // The next two numbers represent the horizontal and vertical 
        // spacing between the components.
        JPanel panel1 = new JPanel(new GridLayout(1,3,2,2));
        JPanel panel2 = new JPanel(new GridLayout(1,2,2,2));
        JPanel panel3 = new JPanel(new GridLayout(2,1,2,2));
        JPanel panel4 = new JPanel(new GridLayout(1,2,2,2));
        
        // 3. Prepare panel1 with labels 1, 2, and 3.
        setLabel(panel1, "Label1");
        setLabel(panel1, "Label2");
        setLabel(panel1, "Label3");
        
        // 4. Prepare panel2 with labels 4 and 5.
        setLabel(panel2, "Label4");
        setLabel(panel2, "Label5");
        
        // 5. Prepare panel3 with labels 6 and 7.
        setLabel(panel3, "Label6");
        setLabel(panel3, "Label7");
        
        // 6. Add panel2 and panel3 to panel4.
        panel4.add(panel2);
        panel4.add(panel3);
        
        // 7. Finally, add panel1 and panel4 to the content pane.
        container.add(panel1);
        container.add(panel4);
       
    }
    
    // 8. Supporting method to create and attach a label.
    public void setLabel(Container panel, String text) {
        JLabel label = new JLabel(text, JLabel.CENTER);
        label.setOpaque(true);
        label.setForeground(Color.white);
        label.setBackground(Color.gray);
        panel.add(label);
    }
}


Tidak ada komentar: