Sei sulla pagina 1di 24

Control Fundamentals

Controls are components that allow a user to interact with your application in various ways. Example- Push button

A layout manager automatically positions components within a container.


Thus, the appearance of a window is determined by a combination of the controls that it contains and the layout manager used to position them.

AWT Controls
The AWT supports the following types of controls: Labels Push buttons Check boxes Choice lists Lists Scroll bars Text editing These controls are subclasses of Component.

Adding and Removing Controls


To include a control in a window, you must add it to the window. To do this, you must first create an instance of the desired control and then add it to a window by calling add( ) which is defined by Container. Component add(Component compObj) Once a control has been added, it will automatically be visible whenever its parent window is displayed.

Contd..
To remove a control from a window when the control is no longer needed, you can call remove( ), which is also defined by Container. void remove(Component obj) You can remove all controls by calling removeAll( )

Labels
A Label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user. Labels defines the following constructors: Label( ) Label(String str) Label(String str, int how) The second version creates a label that contains the string specified by str. This string is left-justified. The third version creates a label that contains the string specified by str using the alignment specified by how. The value of how must be one of these three constants: Label.LEFT, Label.RIGHT, or Label.CENTER.

You can set or change the text in a label by using the setText( ) method. You can obtain the current label by calling getText( ). void setText(String str) String getText( ) You can set the alignment of the string within the label by calling setAlignment( ). To obtain the current alignment call getAlignment( ).

Buttons
A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button. Button defines the following constructors: Button( ) Button(String str) After a button has been created, you can set its label by calling setLabel( ). You can retrieve its label by calling getLabel( ). void setLabel(String str) String getLabel( )

Handling Buttons
Each time a button is pressed, an action event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ActionListener interface. That interface defines the actionPerformed( ) method, which is called when an event occurs. public void actionPerformed(ActionEvent e) An ActionEvent object is supplied as the argument to this method. It contains both a reference to the button that generated the event and a reference to the string that is the label of the button.

Check Boxes
A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a check mark or not. There is a label associated with each check box that describes what option the box represents. You change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class.

Contd..
Checkbox supports these constructors: Checkbox( ) Checkbox(String str) Checkbox(String str, boolean on) Checkbox(String str, boolean on, CheckboxGroup cbGroup) Checkbox(String str,CheckboxGroup cbGroup, boolean on)

The first form creates a check box whose label is initially blank. The state of the check box is unchecked. The second form creates a check box whose label is specified by str. The state of the check box is unchecked. The third form allows you to set the initial state of the check box. If on is true, the check box is initially checked; otherwise, it is cleared.

Contd..
The fourth and fifth forms create a check box whose label is specified by str and whose group is specified by cbGroup. If this check box is not part of a group, then cbGroup must be null. To retrieve the current state of a check box, call getState( ). To set its state, call setState( ). You can obtain the current label associated with a check box by calling getLabel( ). To set the label, call setLabel( ). boolean getState( ) void setState(boolean on) String getLabel( ) void setLabel(String str)

Handling Check Boxes


Each time a check box is selected or deselected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method, which is called when an event occurs. An ItemEvent object is supplied as the argument to this method. It contains information about the event such as whether it was a selection or deselection.

Example
import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class SimpleAWT extends Applet implements ActionListener, ItemListener { private Button button = new Button("Push Me!"); private Checkbox checkbox = new Checkbox("Check Me!"); private Choice choice = new Choice(); private Label label = new Label("Pick something!");public void init() { button.addActionListener(this); checkbox.addItemListener(this); choice.addItemListener(this); // An Applet is a Container because it extends Panel. setLayout(new BorderLayout()); choice.addItem("Red"); choice.addItem("Green"); choice.addItem("Blue");

Panel panel = new Panel(); panel.add(button); panel.add(checkbox); panel.add(choice); add(label, "Center"); add(panel, "South"); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { label.setText("The Button was pushed."); } }

public void itemStateChanged(ItemEvent e) { if (e.getSource() == checkbox) { label.setText("The Checkbox is now " + checkbox.getState() + "."); } else if (e.getSource() == choice) { label.setText(choice.getSelectedItem() + " was selected."); } } }

SimpleAWT.class
<html> <title>The Hello, World Applet</title> <hr> <applet code="SimpleAWT.class" width="320" height="120"> </applet> <hr> </html>

Image Fundamentals

Creating, Loading and Displaying


There are three common operations that occur when you work with images: creating an image loading an image displaying an image In Java, the Image class is used to refer to images in memory and to images that must be loaded from external sources. Thus, Java provides ways for you to create a new image object and ways to load one. It also provides a means by which an image can be displayed .

Creating an Image Object


The Image class doesnt have enough information about its environment to create the proper data format for the screen. Therefore, the Component class in java.awt has a method called createImage( ) that is used to create Image objects. We know that all of the AWT components are subclasses of Component, so all support this method. The createImage( ) method has two forms: Image createImage(ImageProducer imgProd) Image createImage(int width, int height) The first form creates an image produced by imgProd, which is an object of a class that implements the ImageProducer interface. The second form returns a blank image that has the specified width and height.

Contd..
Canvas c = new Canvas( ); Image test = c.createImage(200, 100); This creates an instance of Canvas and then calls the createImage( ) method to actually make an Image object. At this point, the image is blank.

Loading an Image
The other way to obtain an image is to load one. To do this, use the getImage( ) method defined by the Applet class. To load an image in an applet, you can use one of the overloaded getImage( ) methods for loading from a URL: Image getImage(URL url) Image getImage(URL url, String imageName) Image img = getImage("http://www.myschool.edu/anImage.gif"); or : Image img = getImage(getCodeBase(),"anImage.gif"); where the Applet class method getCodeBase( ) provides the URL for the location of the applet's class file.

Contd..
Once you have an image, you can display it by using drawImage( ), which is a member of the Graphics class. It has several forms, but we will be using boolean drawImage(Image imgObj, int left, int top, ImageObserver imgOb) This displays the image passed in imgObj with its upper-left corner specified by left and top. imgOb is a reference to a class that implements the ImageObserver interface. This interface is implemented by all AWT components so you can just put a "this" reference for the argument as default in your program. An image observer is an object that can monitor an image while it loads. To draw an image, use the drawImage( ) method in the Graphics context: g.drawImage( img, x, y, this );

Potrebbero piacerti anche