Rabu, 13 Agustus 2025

JAVA - Box

 





/*

 * 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 tbox1;

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;


public class TBox1 extends JApplet {

    Container container = null;

    

    public void init() {

        // 1. Get the handle on the applet's content pane.

        container = getContentPane();


        // 2. Create a vertical box and add it to the applet.

        Box baseBox = Box.createVerticalBox();

        container.add(baseBox);

             

        // 3. Create the demo panel1 with box layout to display

        // buttons B1 and B2.

        // a) Create the panel and set the box layout.

        JPanel glueDemoPanel1 = new JPanel();

        glueDemoPanel1.setLayout(new BoxLayout(glueDemoPanel1, 

                                     BoxLayout.X_AXIS));

        // b) Set the title border around the panel.

        TitledBorder border1 = new TitledBorder(

                                  new LineBorder(Color.black),

                                  "Glue Component: Demo-1");

        border1.setTitleColor(Color.black);

        glueDemoPanel1.setBorder(border1);

        // c) Add the buttons B1 and B2 with a glue component

        // between them.

        glueDemoPanel1.add(new JButton("B1"));

        glueDemoPanel1.add(Box.createHorizontalGlue());

        glueDemoPanel1.add(new JButton("B2"));


        // d) Add the panel to the base box.

        baseBox.add(glueDemoPanel1);

                

        // 4. Create the demo panel-2, assign box layout and add

        // buttons B3 and B4.

        // a) Create the glue panel 2 and assign the box layout.

        JPanel glueDemoPanel2 = new JPanel();

        glueDemoPanel2.setLayout(new BoxLayout(glueDemoPanel2, 

                                     BoxLayout.X_AXIS));


        // b) Add a titled border to the panel.

        TitledBorder border2 = new TitledBorder(

                                  new LineBorder(Color.black),

                                  "Glue Component: Demo-2");

        border2.setTitleColor(Color.black);

        glueDemoPanel2.setBorder(border2);


        // c) Add the buttons B3 and B4 to the panel; also add

        // the glue components between the buttons, and the

        // buttons and panel.

        glueDemoPanel2.add(Box.createHorizontalGlue());      

        glueDemoPanel2.add(new JButton("B3"));

        glueDemoPanel2.add(Box.createHorizontalGlue());

        glueDemoPanel2.add(new JButton("B4"));

        glueDemoPanel2.add(Box.createHorizontalGlue());


        // d) Add the panel to the base box.

        baseBox.add(glueDemoPanel2);


        // 5. Create a filler panel and add buttons B5 and B6.

        // a) Create the panel object and assign box layout.

        JPanel fillerDemoPanel = new JPanel();

        fillerDemoPanel.setLayout(new BoxLayout(fillerDemoPanel, 

                                     BoxLayout.X_AXIS));


        // b) Set the titled border to the above panel.

        TitledBorder border3 = new TitledBorder(

                                  new LineBorder(Color.black),

                                  "Filler Component Demo");

        border3.setTitleColor(Color.black);

        fillerDemoPanel.setBorder(border3);


        // c) Add buttons B5 and B6 to the panel; also add the

        // filler component between the buttons.

        fillerDemoPanel.add(new JButton("B5"));

        fillerDemoPanel.add(new Box.Filler(

                            new Dimension(50,75), // Minimum Size

                            new Dimension(50,75), // Preferred Size

                            new Dimension(Short.MAX_VALUE,75))); 

                                                    // Maximum Value 

        fillerDemoPanel.add(new JButton("B6"));


        // Attach the panel to the base box.

        baseBox.add(fillerDemoPanel);

                      

        // 6. Create rigid area panel and add buttons B7 and B8.

        // a) Create a panel and assign the box layout.

        JPanel rigidADemoPanel = new JPanel();

        rigidADemoPanel.setLayout(new BoxLayout(rigidADemoPanel, 

                                     BoxLayout.X_AXIS));


        // b) Assign the title border around the panel.

        TitledBorder border4 = new TitledBorder(

                                  new LineBorder(Color.black),

                                  "Rigid Area Component Demo");

        border4.setTitleColor(Color.black);

        rigidADemoPanel.setBorder(border4);


        // c) Add buttons B7 and B8 to the rigid area panel.

        // Also add a rigid area in the middle of the buttons.

        rigidADemoPanel.add(new JButton("B7"));

        rigidADemoPanel.add(Box.createRigidArea(new

Dimension(150,0)));

        rigidADemoPanel.add(new JButton("B8"));


        // d) Add the panel to the base box.

        baseBox.add(rigidADemoPanel);

        

        // 7. Create the strut panel, assign the box layout, and

        // add the buttons B9 and B10.

        // a) Create the panel and assign the box layout

        JPanel strutDemoPanel = new JPanel();

        strutDemoPanel.setLayout(new BoxLayout(strutDemoPanel, 

                                     BoxLayout.X_AXIS));


        // b) Set the titled border around the panel.

        TitledBorder border5 = new TitledBorder(

                                  new LineBorder(Color.black),

                                  "Strut Component Demo");

        border5.setTitleColor(Color.black);


        // c) Add the buttons B9 and B10 to the panel. Also assign

        // the horizontal strut in the middle of the buttons.

        strutDemoPanel.setBorder(border5); 

        strutDemoPanel.add(new JButton("B9"));

        strutDemoPanel.add(Box.createHorizontalStrut(150));

        strutDemoPanel.add(new JButton("B10"));


        // d) Add the panel to the base box.

        baseBox.add(strutDemoPanel);         

    }

}


Tidak ada komentar: