Полезная информация

Exploring Java

Previous: 15.6 Nonstandard Layout ManagersChapter 15
Layout Managers
Next: 16. Drawing with AWT
 

15.7 Absolute Positioning?

It's possible to set the layout manager to null: no layout control. You might do this to position an object on the display at some absolute coordinates. This is almost never the right approach. Components might have different minimum sizes on different platforms, and your interface would not be very portable.

The following applet doesn't use a layout manager and works with absolute coordinates instead:

import java.awt.*; 
 
public class MoveButton extends java.applet.Applet {  
    Button button = new Button("I Move"); 
 
    public void init() { 
        setLayout( null ); 
        add( button ); 
        button.setSize( button.getPreferredSize() ); 
        button.move( 20, 20); 
    } 
 
    public boolean mouseDown( Event e, int x, int y ) { 
        button.move( x, y ); 
        return ( true ); 
    } 
 
} 

Click in the applet area, outside of the button, to move the button to a new location. If you are running the example in an external viewer, try resizing the window and note that the button stays at a fixed position relative to the display origin.


Previous: 15.6 Nonstandard Layout ManagersExploring JavaNext: 16. Drawing with AWT
15.6 Nonstandard Layout ManagersBook Index16. Drawing with AWT

Other Books in this LibraryJava in a NutshellJava Language ReferenceJava AWTJava Fundamental ClassesExploring Java