Advertisement
Constructor | Description |
---|---|
public Checkbox() | Creates a checkbox with no text, this checkbox is unchecked by default.. |
public Checkbox(String text) | Creates a checkbox with a text, this checkbox is unchecked by default.. |
public Checkbox(String text, boolean b) | Creates a checkbox with a text, this checkbox is checked or unchecked depending on the boolean value. |
Methods | Description | public void setName(String text) | Sets a name on the Checkbox, this name will not be displayed. |
---|---|
public String getName() | Gets a String message of Checkbox, this name will not be displayed. |
public void setLabel() | Sets a String text on button. |
public String getLabel() | Gets the String text of this button. |
public void setState(boolean b) | Sets a state of Checkbox. |
public boolean getState() | Gets the state of Checkbox. |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class CheckboxEx1
{
JFrame jf;
Checkbox chk1, chk2, chk3, chk4;
CheckboxEx1()
{
jf= new JFrame("Variants of Checkbox");
//Creating an no-message Checkbox
chk1 = new Checkbox();
//Creating a pre-selected Checkbox with a message
chk2 = new Checkbox("Yes",true);
//Creating an unselected Checkbox with a message
chk3 = new Checkbox("No");
jf.add(chk1);
jf.add(chk2);
jf.add(chk3);
jf.setLayout(new FlowLayout());
jf.setSize(300,120);
jf.setVisible(true);
}
public static void main(String... ar)
{
new CheckboxEx1();
}
}
Advertisement
import java.awt.*;
import java.awt.event.*;
public class CheckboxEx2 implements ItemListener
{
Frame jf;
Checkbox chk1, chk2;
Label label1;
CheckboxEx2()
{
jf= new Frame("Checkbox");
chk1 = new Checkbox("Water");
chk2 = new Checkbox("Coffee");
label1 = new Label();
jf.add(chk1);
jf.add(chk2);
chk1.addItemListener(this);
chk2.addItemListener(this);
jf.setLayout(new FlowLayout());
jf.setSize(232,150);
jf.setVisible(true);
}
public void itemStateChanged(ItemEvent ie)
{
Checkbox ch =(Checkbox)ie.getItemSelectable();
if(ch.getState()==true)
{
label1.setText(ch.getLabel()+ " is checked");
jf.add(label1);
jf.setVisible(true);
}
else
{
label1.setText(ch.getLabel()+ " is unchecked");
jf.add(label1);
jf.setVisible(true);
}
}
public static void main(String... ar)
{
new CheckboxEx2();
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement