Containers and layouts in Java:
We will start by looking at how visual components are placed in a component known as a container. Later on in this section we shall look at how to create a button so that we can use it as an example of a visual component to illustrate various layouts.
In this course, you will meet three containers and these are as follows.
- JFrame. This is a window used in Java applications.
- JApplet. This is a container that can be embedded in a web page (applets will be considered in another unit).
- JPanel. This is a container that can be used in both applications and applets. Panels, like all containers, can contain both visual components and other containers. You will find yourself using instances of the JPanel class very frequently as they provide a large amount of fiexibility in laying out visual components.
The container JFrame represents an application window that is represented on a display device and with which the user can interact. A JFrame is able to nest (contain) other components. The actual elements in the JFrame are held in its content pane, which is the usable area of the frame in which other components can be placed. It does not include the title bar along the top of the frame containing the 'coffee cup' icon (or any other icon that the programmer chooses - see the Java API documentation if you wish to follow this up), the title of the frame (again set by the programmer) and the standard window buttons.
The interface for an application is usually created by inheriting from JFrame. This allows the application to inherit all of the non-private methods within JFrame. There are many inherited methods but we will look at the most commonly used ones, as listed in Table.
![1955_method of class jfram.png](https://www.expertsmind.com/CMSImages/1955_method%20of%20class%20jfram.png)
As you will see later, many of these methods are also used with JPanel. A panel is a container that can contain other containers and also GUI visual components such as buttons. Thus panels allow you to modularize the layout of the GUI. A frame is a window in which containers (such as panels) and visual components (such as buttons) can be embedded. These correspond to the normal windows you see when using an operating system such as Windows.
The way in which visual components are laid out inside a container depends on the way you specify its layout. In order for you to communicate your requirements, layout managers provide a number of patterns that you can adopt.
Each container has a method called setLayout, which allows you to specify the pattern of layout for the visual components within the container. There are a number of standard layout patterns that can be set for a container but each type of container has a default pattern. The container JFrame has a default pattern of BorderLayout and JPanel has a default of FlowLayout.
At this stage, do not worry about what the BorderLayout or the FlowLayout does; all you need to know is that the code says that when you add components they will obey the BorderLayout rules or the FlowLayout rules. We shall discuss the various layouts later in this unit.
We will give a series of code fragments or small applications to cover specific topics. We won't cover every element in the Swing library, but there will be enough to give you a solid foundation and allow you to access the Swing library with confidence. When you start to use the Swing classes you will almost certainly begin to develop your own personal style of writing user interfaces but, until that happens, the most effective way to progress is to experiment liberally with the classes until you find something that you like and which solves the particular problem in hand.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Containers and layouts in Java homework and assignments? Live Containers and layouts in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Containers and layouts in Java homework help, java assignment help and Containers and layouts in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours