Rabu, 13 Agustus 2025

JAVA - BORDER LAYOUT

 




/*

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

import java.awt.*;

import javax.swing.*;


public class TBorderLayout extends JApplet {

    Container container = null;


    public void init() {

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

       container = this.getContentPane();

       // container.setLayout(new BorderLayout());

       /* Note: Don't have to explicitly set border layout,

          because the content pane uses border layout

          by default */


       // 2. Give some horizontal and vertical gaps between buttons.

       ((BorderLayout) container.getLayout()).setHgap(2);

       ((BorderLayout) container.getLayout()).setVgap(2);


       // 3. Array that contains border layout constants.

       String[] borderConsts = { BorderLayout.NORTH,

                                 BorderLayout.SOUTH,

                                 BorderLayout.EAST,

                                 BorderLayout.WEST,

                                 BorderLayout.CENTER };


       // 4. Array that contains button names.

       String[] buttonNames = { "North Button", "South Button",

                                "East Button", "West Button",

                                "Center Button" };


       // 5. Create and add buttons to the container.

       for (int i=0; i<borderConsts.length; i++) {

          JButton button = new JButton(buttonNames[i]);


          // You may wish to reset the preferred size of the east

          // and west buttons to display wide labels.

          if (buttonNames[i] == "East Button" ||

              buttonNames[i] == "West Button") {

              button.setPreferredSize(new Dimension(115, 25)); 

              // width=115, height=25

          }

          container.add(button, borderConsts[i]);

       }

    }

}







Tidak ada komentar: