import java.applet.*; import java.awt.*; public class GUIexample2 extends Applet { Button mbutton; Label mlabel; Label eid; Label mousexy; List mlist; Choice mchoice; CheckboxGroup mradio; Checkbox radio1; Checkbox radio2; Checkbox radio3; Checkbox mcheckbox; TextField mtext; Scrollbar mscroll; public void init() { mbutton = new Button("Press here!"); mlabel= new Label (" "); eid= new Label ("Last event id was 0 "); mousexy = new Label ("Mouse at x=0 y=0 "); mlist = new List (2,true); mlist.addItem("Perl"); mlist.addItem("C"); mlist.addItem("C++"); mlist.addItem("Java"); mchoice = new Choice(); mchoice.addItem("Red"); mchoice.addItem("Blue"); mchoice.addItem("Black"); mchoice.addItem("White"); mradio = new CheckboxGroup(); radio1 = new Checkbox("All",mradio,true); radio2 = new Checkbox("Odd",mradio,false); radio3 = new Checkbox("Even",mradio,false); mcheckbox = new Checkbox("This option?"); mtext = new TextField("",20); mscroll = new Scrollbar(Scrollbar.HORIZONTAL,50,0,1,100); Panel p1 = new Panel(); p1.setLayout(new PackerLayout()); p1.add("r1;side=top;anchor=w",radio1); p1.add("r2;side=top;anchor=w",radio2); p1.add("r3;side=top;anchor=w",radio3); Panel p2 = new Panel(); p2.setLayout(new PackerLayout()); p2.add("c1;side=top",mchoice); p2.add("l1;side=top",mlist); Panel p3 = new Panel(); p3.setLayout(new PackerLayout()); p3.add("b1;side=left;expand=true;fill=x;anchor=w",mcheckbox); p3.add("b2;side=right;expand=true;fill=x;anchor=w",mbutton); Panel p4 = new Panel(); p4.setLayout(new PackerLayout()); p4.add("p4;side=bottom;expand=true;fill=x",p3); p4.add("s1;side=bottom;expand=true;fill=x",mscroll); p4.add("l1;side=bottom;expand=true;fill=x",mlabel); Panel p5 = new Panel(); p5.setLayout(new PackerLayout()); p5.add("p1;side=left;anchor=s",p1); p5.add("p2;side=left;anchor=s",p2); p5.add("p4;side=left;anchor=s;padx=5",p4); Panel p6 = new Panel(); p6.setLayout(new PackerLayout()); p6.add("l2;side=left;expand=true;fill=x",mousexy); p6.add("l2;side=right;expand=true;fill=x",eid); this.setLayout(new PackerLayout()); this.add("p5;side=bottom;anchor=s",p5); this.add("p6;side=top;anchor=n",p6); } public boolean handleEvent(Event e) { Object arg = e.arg; eid.setText("Last event id was "+String.valueOf(e.id)); mousexy.setText("Mouse at x="+String.valueOf(e.x)+" y="+String.valueOf(e.y)); switch (e.id) { case Event.ACTION_EVENT: if(e.target==mbutton) { mlabel.setText("You pressed the button!"); return true; } if(e.target==mcheckbox) { mlabel.setText("You pressed the checkbox!"); return true; } if(e.target==radio1) { mlabel.setText("You selected All!"); return true; } if(e.target==radio2) { mlabel.setText("You selected Odd!"); return true; } if(e.target==radio3) { mlabel.setText("You selected Even!"); return true; } if(e.target==mchoice) { mlabel.setText("You selected "+mchoice.getSelectedItem()+"!"); return true; } if(e.target==mtext) { mlabel.setText("You entered "+mtext.getText()+"!"); return true; } break; case Event.LIST_SELECT: if(e.target==mlist) { mlabel.setText("You selected "+arg.toString()+" !"); return true; } break; case Event.LIST_DESELECT: if(e.target==mlist) { mlabel.setText("You deselected "+arg.toString()+"!"); return true; } break; case Event.SCROLL_LINE_UP: case Event.SCROLL_LINE_DOWN: case Event.SCROLL_PAGE_UP: case Event.SCROLL_PAGE_DOWN: case Event.SCROLL_ABSOLUTE: if(e.target==mscroll) { mlabel.setText("New value of scrollbar is "+ String.valueOf(mscroll.getValue())+"!"); return true; } break; case Event.MOUSE_DOWN: mlabel.setText("Mouse button was pressed!"); return true; case Event.MOUSE_UP: mlabel.setText("Mouse button was released!"); return true; case Event.MOUSE_DRAG: mlabel.setText("Mouse was dragged!"); return true; } return super.handleEvent(e); } }