Wednesday, November 5, 2014

Java creat pro



Create AWT Checkbox

import java.applet.Applet;
import java.awt.Checkbox;
public class CreateCheckBox extends Applet
{
public void init()
{
Checkbox checkBox1 = new Checkbox();         
checkBox1.setLabel("My Checkbox 1"); Checkbox checkbox2 = new Checkbox("My Checkbox 2");
add(checkBox1);
add(checkbox2);
   }
}

Create Checked Checkbox

import java.applet.Applet;
import java.awt.Checkbox;
public class CreateCheckedCheckBox extends Applet
{
public void init()
{
Checkbox checkBox1 = new Checkbox("My Checkbox 1");  Checkbox checkbox2 = new Checkbox("My Checkbox 2", true);
add(checkBox1);
add(checkbox2);
    }
}

Determine If The Checkbox Is Enabled

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Checkbox;
public class DetermineIfCheckboxEnabled extends Applet
{      
boolean isCheckbox1Enabled;
boolean isCheckbox2Enabled;
public void init()
{
Checkbox Checkbox1 = new Checkbox("Checkbox 1");
Checkbox Checkbox2 = new Checkbox("Checkbox 2");     
add(Checkbox1);
add(Checkbox2);     
Checkbox1.setEnabled(false);
isCheckbox1Enabled = Checkbox1.isEnabled();
isCheckbox2Enabled = Checkbox2.isEnabled();         
public void paint(Graphics g)
{
g.drawString("Is Checkbox 1 enabled? " + isCheckbox1Enabled, 10,50);
g.drawString("Is Checkbox 2 enabled? " + isCheckbox2Enabled, 10,70);
  }
}

Determine If The Checkbox Is Visible

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Checkbox;
public class DetermineIfCheckboxVisible extends Applet
{
boolean isCheckbox1Visible;
boolean isCheckbox2Visible;
public void init()
{
Checkbox Checkbox1 = new Checkbox("Checkbox 1");
Checkbox Checkbox2 = new Checkbox("Checkbox 2");
add(Checkbox1);
add(Checkbox2);       
Checkbox2.setVisible(false);           
isCheckbox1Visible = Checkbox1.isVisible();
isCheckbox2Visible = Checkbox2.isVisible();            
}     
public void paint(Graphics g)
{
g.drawString("Is Checkbox 1 visible? " + isCheckbox1Visible, 10,50);
g.drawString("Is Checkbox 2 visible? " + isCheckbox2Visible, 10,70);
  }
}

Disable Checkbox

import java.applet.Applet;
import java.awt.Checkbox;
public class DisableCheckboxExample extends Applet
{
public void init()
{
Checkbox Checkbox1 = new Checkbox("Checkbox 1");
Checkbox Checkbox2 = new Checkbox("Checkbox 2");
add(Checkbox1);
add(Checkbox2);
Checkbox2.setEnabled(false);
   }
}

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();             
   }
}

No comments:

Post a Comment