Apr 16, 2012

Button Class


A button is a component that has a label and can respond when pressed.
Button()              - Construct a button with no label
Button(String label)        - Construct a button with specified label

Methods

setLabel(String label) – It sets the label to the specified string
getLabel()          - Gets the label from the button.

Example:
 import java.awt.*;

class frameDemo
{
public static void main(String[] args)
        {
            Button b;
            Frame f=new Frame(“Button Demo”);
            f.setSize(250,150);
            b=new Button(“Click me”);
            f.add(b);
            f.setLocation(200,200);
            f.setVisible(true);
            System.out.println(“Return to DOS prompt and press ctrl+C to quit”);
        }
}

output:



The setLocation(int x, int y) –   This method displays frame located upper left corner at x,y.
The add() method adds the button to the frame.

Note: Button fills the entire Frame. Because, A frame’s default Layout manager is always Border Layout and its default constraint is BorderLayout.CENTER.



0 comments :

Post a Comment