Question
Program:
//Displays a frame containing a three CheckBox. Tick the
//CheckBox causes the background of the frame to change from
//white to define color.
import java.awt.*;
import java.awt.event.*;
import javafx.scene.control.CheckBox;
public class checkboxcolor {
public static void main(String[] args) {
Frame f = new ChangecheckboxColorFrame("Check Box");
f.setSize(220, 200);
f.setVisible(true);
}
}
//Frame class
class ChangecheckboxColorFrame extends Frame {
Checkbox checkBox1 = new Checkbox("Red",false);
Checkbox checkBox2 = new Checkbox("Green",false);
Checkbox checkBox3 = new Checkbox("Blue",false);
public ChangecheckboxColorFrame(String title) {
super(title);
setLayout(null);
setBackground(Color.white);
//Add Checkbox to frame and attach listener
checkBox1.setBounds(10,150, 80,50);
checkBox2.setBounds(90,150, 80,50);
checkBox3.setBounds(170,150, 80,50);
add(checkBox1);
checkBox1.addItemListener(new ColorItemListener());
add(checkBox2);
checkBox2.addItemListener(new ColorItemListener());
add(checkBox3);
checkBox3.addItemListener(new ColorItemListener());//Attach //window listener
addWindowListener(new WindowCloser());
}
//Listener for checkbox
class ColorItemListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
boolean a1=checkBox1.getState();
boolean a2=checkBox2.getState();
boolean a3=checkBox3.getState();
if(a1==true && a2==true && a3==true) {
setBackground(Color.WHITE);
}
else if(a1==true) {
if(a2==true) {
setBackground(Color.YELLOW);
}
else if(a3==true) {
setBackground(Color.MAGENTA);
}
else {
setBackground(Color.RED);
}
}
else if(a2==true) {
if(a3==true) {
setBackground(Color.CYAN);
}
else{
setBackground(Color.GREEN);
}
}
else if(a3==true) {
setBackground(Color.BLUE);
}
else {
setBackground(Color.BLACK);
}
}
}
//Listener for window
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
the better one
ReplyDeletepackage termwork5;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class cl{
public static int r=0,g=0,b=0;
}
public class pgm3 {
public static void main(String[] args) {
Frame f =new Frame("Random color");
f.setSize(300,200);
f.setVisible(true);
f.setLayout(null);
f.addWindowListener(new close());
Checkbox c1=new Checkbox("Red", false);
Checkbox c2=new Checkbox("Green", false);
Checkbox c3=new Checkbox("Blue", false);
c1.setBounds(40, 150, 60, 30);
c2.setBounds(120, 150, 60, 30);
c3.setBounds(190, 150, 60, 30);
c1.addItemListener(new checkcolor(f,c1,c2,c3));
c2.addItemListener(new checkcolor(f,c1,c2,c3));
c3.addItemListener(new checkcolor(f,c1,c2,c3));
f.add(c1);
f.add(c2);
f.add(c3);
}
}
class checkcolor implements ItemListener{
Frame f;
Checkbox c1,c2,c3;
public checkcolor(Frame f,Checkbox c1,Checkbox c2,Checkbox c3) {
// TODO Auto-generated constructor stub
this.f=f;
this.c1=c1;
this.c2=c2;
this.c3=c3;
}
boolean r,g,b;
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
r=c1.getState();
g=c2.getState();
b=c3.getState();
if(r==true)
cl.r=255;
else
cl.r=0;
if(g==true)
cl.g=255;
else
cl.g=0;
if(b==true)
cl.b=255;
else
cl.b=0;
f.setBackground(new Color(cl.r, cl.g, cl.b));
}
}
Ok thanks
ReplyDelete