Most Read This Week
Lesson 12: An Introduction to Graphical User Interfaces with Swing - Part I
Everything is a little better, faster, and more convenient in Swing
By: Yakov Fain
Aug. 18, 2005 09:00 AM
The next two lines of code set a box layout with vertical alignment in a JPanel.
JPanel p1= new JPanel();
To make this code shorter, I do not declare a variable to store a reference to the object BoxLayout, but rather create an instance of this object and immediately pass it to the method setLayout() as an argument.
Grid Bag Layout
Our calculator has rows and columns, but in a grid layout, all of the components must have the same size. This does not work for our calculator because there is a text field on the top that is as wide as tree numeric buttons.
The GridBagLayout is an advanced grid, that allows you to have a grid with cells of different sizes. Class GridBagLayout works together with another class called GridBagConstraints. Constrains is nothing else but attributes of the cell, and you have to set them for each cell separately. All constraints for a cell have to be set before placing a component in the cell. For example, one of the constraint's attributes is called gridwidth. It allows you to make a cell as wide as several other cells.
When working with the grid layout you should create an instance of the constraint object first, and then set the values to its properties. After this is done, you can add the component to the cell in your container.
The next code sample is heavily sprinkled with comments to help you understand how to use GridBagLayout.
The spring layout is based on the principles of actual springs and struts. Imagine that you have a bunch of hooks, springs and struts and need to fix our buttons and text fields to the edges of the window. Components that are attached with struts, won't move and change size if you start stretching the window, but those with springs can grow.
The distances between the components are defined by spring's properties (minimum, maximum, preferred and actual size). These parameters are specified in the SpringLayout.Constraints object for each component. This makes it somewhat similar to the GridBagLayout.
As opposed to GridLayout, SpringLayout allows you to arrange components of different sizes but maintain their relative positions.
You can read more on this layout in the article by Joe Winchester "SpringLayout: A Powerful and Extensible Layout Manager" (http://jdj.sys-con.com/read/37300.htm ).
When you click on a tab, the content of the screen changes. In fact, all of the panels needed for this screen are already pre-loaded and lay on top of each other. When the user clicks on a tab, the program just "brings this card" on top and makes the rest of the cards invisible.
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads