// *******************************************************************

// Swing5.java By: Aiman Hanna (C) 1993 - 2018

// This program introduces Swing containers and layout managers.

//

//

// Key Points:

// 1) The BorderLayout Manager.

// *******************************************************************

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class ExitListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

// This class is needed to create BorderLayout frames

class JFrame3 extends JFrame

{

// Attributes

public static final int WIDTH = 500;

public static final int HIGHT = 200;

// Default Constructor

public JFrame3()

{

super(); // Calls the constructor of JFrame

setSize(WIDTH, HIGHT);

setTitle("Window Frame");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Sets the frame layout to a border layout

setLayout(new BorderLayout());

}

// Parameterized Constructor

public JFrame3(String t)

{

super(); // Calls the constructor of JFrame

setSize(WIDTH, HIGHT);

setTitle(t);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Sets the frame layout to a border layout

setLayout(new BorderLayout());

}

// Parameterized Constructor

public JFrame3(String t, int w, int h)

{

super(); // Calls the constructor of JFrame

setSize(w, h);

setTitle(t);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Sets the frame layout to a border layout

setLayout(new BorderLayout());

}

// Parameterized Constructor

public JFrame3(int w, int h)

{

super(); // Calls the constructor of JFrame

setSize(w, h);

setTitle("Window Frame");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Sets the frame layout to a border layout

setLayout(new BorderLayout());

}

public void colorWindow(Color cl)

{

getContentPane().setBackground(cl);

}

// A method that adds a given label to a specific location on the layout

public void addLabel(JLabel lb, String layoutLocation)

{

if(layoutLocation.equalsIgnoreCase("south"))

this.add(lb, BorderLayout.SOUTH);

else if(layoutLocation.equalsIgnoreCase("north"))

this.getContentPane().add(lb, BorderLayout.NORTH);

else if(layoutLocation.equalsIgnoreCase("west"))

this.add(lb, BorderLayout.WEST);

else if(layoutLocation.equalsIgnoreCase("east"))

this.add(lb, BorderLayout.EAST);

else // this will accommodate all other input errors as well; just add to center

this.add(lb, BorderLayout.CENTER);

}

// A method that adds a given button to a specific location on the layout

public void addButton(JButton bt, String layoutLocation)

{

if(layoutLocation.equalsIgnoreCase("south"))

this.add(bt, BorderLayout.SOUTH);

else if(layoutLocation.equalsIgnoreCase("north"))

this.add(bt, BorderLayout.NORTH);

else if(layoutLocation.equalsIgnoreCase("west"))

this.add(bt, BorderLayout.WEST);

else if(layoutLocation.equalsIgnoreCase("east"))

this.add(bt, BorderLayout.EAST);

else // this will accommodate all other input errors as well; just add to center

this.add(bt, BorderLayout.CENTER);

}

}

public class Swing5{

public static void main(String[] args)

{

JFrame3 window1 = new JFrame3();

window1.colorWindow(Color.pink);

window1.addLabel(new JLabel("Hello"), "north");

window1.addLabel(new JLabel("Welcome to"), "west");

window1.addLabel(new JLabel(" Border"), "center");

window1.addLabel(new JLabel("Layout"), "east");

JButton exitButton = new JButton("Click Here to Exit!");

exitButton.addActionListener( new ExitListener());

window1.addButton(exitButton, "south");

JFrame3 window2 = new JFrame3("Welcome to Layout Illustration", 800, 200);

window2.colorWindow(Color.CYAN);

window2.addLabel(new JLabel("Which of the Above Buttons will Exit the Program?"), "south");

JButton nButton = new JButton("North Click to Exit!!! Will You?");

JButton eButton = new JButton("East Click to Exit!!! Will You?");

JButton wButton = new JButton("West Click to Exit!!! Will You?");

JButton cButton = new JButton("Center Click to Exit!!! Will You?");

wButton.addActionListener( new ExitListener());

window2.addButton(nButton, "north");

window2.addButton(eButton, "east");

window2.addButton(cButton, "center");

window2.addButton(wButton, "west");

window1.setVisible(true);

window2.setVisible(true);

}

}

/* The Output - You need to run the program to see its behavior

*/