Get Checkbox State
import
java.applet.Applet;
import
java.awt.Checkbox;
import
java.awt.Graphics;
public class
GetCheckboxState extends Applet
{
Checkbox checkBox1 =
null;
Checkbox checkBox2 =
null;
public void
init(){checkBox1 = new Checkbox("My Checkbox 1"); checkBox2 = new
Checkbox("My Checkbox 2", true);
add(checkBox1);
add(checkBox2);
}
public void
paint(Graphics g)
{
g.drawString("Is
checkbox 1 selected? " + checkBox1.getState(), 10, 80);
g.drawString("Is
checkbox 2 selected? " + checkBox2.getState(), 10, 100);
}
}
Handle Checkbox Event
import
java.applet.Applet;
import
java.awt.Checkbox;
import
java.awt.Graphics;
import
java.awt.event.ItemEvent;
import
java.awt.event.ItemListener;
public class
HandleCheckboxEvent extends Applet implements ItemListener
{
Checkbox java = null;
Checkbox vb = null;
Checkbox c = null;
public void init()
{
java = new
Checkbox("Java");
vb = new
Checkbox("Visual Basic");
c = new
Checkbox("C");
add(java);
add(vb);
add(c);
java.addItemListener(this);
vb.addItemListener(this);
c.addItemListener(this);
}
public void
paint(Graphics g)
{
g.drawString("Java:
" + java.getState(),10,80);
g.drawString("VB:
" + vb.getState(), 10, 100);
g.drawString("C:
" + c.getState(), 10, 120);
}
public void
itemStateChanged(ItemEvent ie)
{
repaint();
}
}
Hide Checkbox
import
java.applet.Applet;
import
java.awt.Checkbox;
public class
HideCheckboxExample extends Applet
{
public void init()
{
Checkbox Checkbox1 =
new Checkbox("Checkbox 1");
Checkbox Checkbox2 =
new Checkbox("Checkbox 2");
add(Checkbox1);
add(Checkbox2);
Checkbox2.setVisible(false);
}
}
Remove AWT Checkbox From Applet or Window Frame
import
java.applet.Applet;
import
java.awt.Checkbox;
public class
RemoveCheckboxExample extends Applet
{
public void init()
{
Checkbox Checkbox1 =
new Checkbox("Checkbox 1");
Checkbox Checkbox2 =
new Checkbox("Checkbox 2");
add(Checkbox1);
add(Checkbox2);
remove(Checkbox1);
}
}
Change Checkbox Background Color
import
java.applet.Applet;
import
java.awt.Checkbox;
import
java.awt.Color;
public class
ChangeCheckboxBackgroundExample extends Applet
{
public void init()
{
Checkbox checkbox1 =
new Checkbox("Checkbox 1");
Checkbox checkbox2 =
new Checkbox("Checkbox 2");
checkbox1.setBackground(Color.red);
checkbox2.setBackground(Color.green);
add(checkbox1);
add(checkbox2);
}
}
Change Checkbox Font
import
java.applet.Applet;
import
java.awt.Checkbox;
import java.awt.Font;
public class
ChangeCheckboxFontExample extends Applet
{
public void init()
{
Checkbox checkbox1 =
new Checkbox("Checkbox 1");
Checkbox checkbox2 =
new Checkbox("Checkbox 2");
Font myFont = new
Font("Courier", Font.ITALIC,12);
checkbox1.setFont(myFont);
add(checkbox1);
add(checkbox2);
}
}
Change Checkbox Foreground Color
import
java.applet.Applet;
import
java.awt.Checkbox;
import java.awt.Color;
public class
ChangeCheckboxForegroundExample extends Applet
{
public void init()
{
Checkbox checkBox1 =
new Checkbox("Checkbox 1");
Checkbox checkBox2 =
new Checkbox("Checkbox 2");
checkBox1.setForeground(Color.magenta);
checkBox2.setForeground(Color.blue);
add(checkBox1);
add(checkBox2);
}
}
No comments:
Post a Comment