Sunday, February 12, 2012

Using absolute positioning

All Java components have a platform-dependent, preferred size. This specifies how large the component should be, notwithstanding the layout policy of the layout manager used by its container. A component's preferred size is usually the size that is big enough for a user to see its function on a GUI.
You can use absolute positioning in conjunction with a layout manager to position components. When using absolute positioning, you are required to specify the size and position of each component within the container. You can modify a component's position and size by using the Component class's setBounds method.

public void setBounds(int x, int y, int width, int height);

When a layout manager lays out components, it takes into account its own layout policy and the preferred sizes of its container's components.

If there is a clash between the layout policy of the layout manager and the preferred size of a component that it is positioning, the layout policy takes precedence.

For example, if a button's preferred size is larger than the size and position required by the layout manager's layout policy, the button will be resized by the layout manager.

The Component class has instance variables that specify the size and position of components.
The variables x and y specify the component's position in pixels, relative to the top left-hand corner of its container.

The width and height variables are also measured in pixels.
To use absolute positioning, you need to set the container's layout property to null.

public class LayoutApplet extends Applet {
import java.awt.*;
import java.applet.Applet;

public class NewButton extends Applet {
  public void init (){
    MISSING CODE
  }
}
public class LayoutApplet extends Applet {
import java.awt.*;
import java.applet.Applet;

public class NewButton extends Applet {
  public void init (){
    setLayout (null);
  }
}

You type setLayout (null); to set the layout property to null.
To set the container's layout property to null, you call the setLayout method and give it a parameter of null.

import java.awt.*;
import java.applet.Applet;

public class NewButton extends Applet {
  public void init (){
    setLayout (null);
  }
}

Note

Using absolute positioning for all components on your interface is not advised because components aren't implemented consistently across platforms. Components also don't resize well when you resize the top-level container. You should use absolute positioning only when it is really necessary.
Consider the code in which you add a button to the applet NewButton. Its position is four pixels in and six pixels down from the top left-hand corner of the container. You also specify the size of the button – 200 pixels wide and 400 pixels high.
import java.awt.*;
import java.applet.Applet;

public class NewButton extends Applet {
  public void init (){
    setLayout (null) ;
    Button b = new Button ("Exit");
    b.setBounds(4,6,200,400);
    add(b);
  }
}
When you run the NewButton applet, the button appears the exact size defined in the setBounds method. This is because the applet is not using the layout manager class.

This may cause only part of the button to be shown or the button may not be visible at all. You may need to resize the JFrame window to see the button.

import java.awt.*;
import java.applet.Applet;

public class NewButton extends Applet {
  public void init (){
    setLayout (null) ;
    Button b = new Button ("Exit");
    b.setBounds(4,6,200,400);
    add(b);
  }
}

23 comments:

I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs. J2EE Training in Chennai | JAVA Training Institutes in Chennai

Excellent post!!! Selenium automation testing tool makes software and web application process lot simpler. Keep on updating your blog with such awesome information. Selenium Course in Chennai

Thanks for sharing such informative article on Loadrunner Automation testing tool. This load testing tool will provide most precise information about the quality of software. Loadrunner Course in Chennai | Loadrunner training institute in Chennai

PHP scripting is definitely one of the easiest, if not the easiest scripting language to learn and grasp for developers. This is partially due to the similarities PHP syntax has with C and Java. Even if the only knowledge of development that you have is with HTML, picking up PHP is still fairly easy.
PHP training in Chennai|PHP training institute in Chennai|PHP course in Chennai

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
Oracle Training In Chennai

Best SAS Training Institute In Chennai It’s too informative blog and I am getting conglomerations of info’s about Oracle interview questions and answer .Thanks for sharing, I would like to see your updates regularly so keep blogging.

Thanks for sharing the wonder article, SAS is the recent technology, you must learn this valuable course to get future in IT firm.
Regards,
SAS Training in Chennai|SAS Training in Chennai

And that I guess I could come up with yet another flawed solution soon. The fact is - there is no clear answer on when to say "today the page is whole" because of the eternal programs performing their work. Pick the one that serves you most readily useful, but beware of its shortcomings.

We utilize ADimensions' course to resize the windowpane.
Allows consider the aid of Selenium WebDrivers Dimensions Course and state item state 'dA by initializing it using thickness and peak as 420X600 . For more info on this go here

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

for preparing bank exam and group exam , we are providing an online test model questions papers

Group Exam Questions and Answers
Bank Exam Questions and Answers

Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
php training in chennai
java training in chennai
dot net training in chennai

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your..
Vmware Training in Chennai

Great article, thanks for sharing usefull information and i have seen more info on
UI online training

Post a Comment