The BorderLayout class Assignment Help

Assignment Help: >> Placing elements using LayoutManager >> The BorderLayout class

The BorderLayout class

We will start with the BorderLayout class. Our example code looks like this:

import java.awt. *;
import javax.swing. *;

public class FrameDemo extends JFrame
{
private final int numOfBut = 5;
private JButton [] buttons = new JButton [numOfBut ];

public FrameDemo (String title)
{
setSize(300, 300);
setTitle(title);

/* Create and name some buttons. */
for (int i = 0; i < numOfBut; i++)
{
buttons [i ] = new JButton("Button " + i);
}

/* Place some buttons. */
Container cp = getContentPane( );
cp.add(buttons [0 ], BorderLayout.NORTH);

cp.add(buttons [1 ], BorderLayout.SOUTH);

cp.add(buttons [2 ], BorderLayout.EAST);

cp.add(buttons [3 ], BorderLayout.WEST);

cp.add(buttons [4 ], BorderLayout.CENTER);
}
}

The code simply fieshes out the previous example for frames and can be tested using the class FrameDemoTest as given previously. We define an array of buttons, each of which is created with a numbered string. We have used the default layout for a JFrame, which is the BorderLayout. The method getContentPane gives us access to the container holding the window components. We then place each button into the content pane.

In fact, it is no longer necessary since Java 5 to invoke the methods add (and its variants), remove and setLayout on a content pane. Instead, they can be invoked directly on a JFrame, and will be invoked on its content pane behind the scenes. We have shown the earlier style here as you are still likely to encounter it in GUI code for some time to come and because it highlights the fact that these methods are actually being performed on a container within the JFrame.

When using the BorderLayout model each element is placed in the frame using a static constant NORTH, EAST, SOUTH, WEST and CENTER. These define where in the frame the objects are placed, following the rough basis of a compass. When you add items to a container you always use an add method.

 

193_border layout class.png

The element, which is placed at the CENTER (note the American spelling) location, is allocated as much space as has been left over by the other elements.

You can see how the elements are placed on the points of a compass. Notice that the two buttons placed into the frame at NORTH and SOUTH are expanded to fill the whole width of the frame.

The BorderLayout can also be used with other components, and not just with JFrame. Since BorderLayout is the default with JFrame, we can use it without having to set the layout style. If we want to use this style with a JPanel component we must explicitly request it.

JPanel p = new JPanel( );

p.setLayout(new BorderLayout( ));

p.add(new JButton("press"), "North");

p.add(new JButton("reporter"), "Sou t h " ) ;

With JPanel there is no content pane to access to add or remove components. Note also that the strings "North", "South" and so on are acceptable in place of constants such as BorderLayout.NORTH.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your The BorderLayout class homework and assignments? Live The BorderLayout class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The BorderLayout class homework help, java assignment help and The BorderLayout class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd