Sunday, October 9, 2011

Change AWT Button Font Example

/*
Change Button Font Example
This java example shows how to change button's font using
AWT Button class.
*/

import java.applet.Applet;
import java.awt.Button;
import java.awt.Font;


/*
<applet code="ChangeButtonFontExample" width=200 height=200>
</applet>
*/
public class ChangeButtonFontExample extends Applet{

public void init(){

//create buttons
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");

/*
* To change font of a button use
* setFont(Font f) method.
*/

Font myFont = new Font("Courier", Font.ITALIC,12);
button1.setFont(myFont);

//add buttons
add(button1);
add(button2);
}
}


Output:



»»  READMORE...

Display Image In An Applet Example

/*
Display Image in an Applet Example
This Java example shows how to display an image using drawImage method
of an Java Graphics class.
*/


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

/*
<applet code = "DisplayImageExample" width = 500 height = 300>
<param name = "Image1" value = "one.jpg">
<param name = "Image2" value = "two.jpg">
</applet>
*/

public class DisplayImageExample extends Applet
{
Image img1, img2;

public void init(){

img1 = getImage(getDocumentBase(), getParameter("Image1"));
img2 = getImage(getDocumentBase(), getParameter("Image2"));
}

public void paint(Graphics g){

//display an image using drwaImage method of Graphics class.
g.drawImage(img1, 0,0,this);
g.drawImage(img2, 100,100,this);
}

}
»»  READMORE...

Create AWT Button Example

/*
Create AWT Button Example 
This java example shows how to create a Button using AWT Button class.
*/

import java.applet.Applet;
import java.awt.Button;


/*
<applet code="CreateAWTButtonExample" width=200 height=200>
</applet>
*/

public class CreateAWTButtonExample extends Applet{

public void init(){

/*
* To create a button use
* Button() constructor.
*/

Button button1 = new Button();

/*
* Set button caption or label using
* void setLabel(String text)
* method of AWT Button class.
*/

button1.setLabel("My Button 1");

/*
* To create button with the caption use
* Button(String text) constructor of
* AWT Button class.
*/

Button button2 = new Button("My Button 2");

//add buttons using add method
add(button1);
add(button2);
}

}


Output:







»»  READMORE...