import java.awt.*; import java.applet.*; //import any other classes you need here //The template class..... Modify this class to make your own applet //by Andy Harris, 4/4/97 //Based on JavaC 1.02 Uses OLD event model!!! //Uses gridbag layout manager. //NOTE: this class is NOT intended to be compiled directly. Use it as a template // for building your own classes. public class Template extends Applet{ //Change Template to the name of your class //The class file name must be EXACTLY the same //as the class name here (case sensitive) with //a .java extension //use this space to define variables used throughout the class //especially components which will have events //uncomment and copy any objects you need, changing the names as necessary //Label lbl1 = new Label("Label caption"); //Button cmd1 = new Button("Click Me"); //TextField txt1 = new TextField("Text Field"); //Scrollbar scr1 = new Scrollbar(Scrollbar.VERTICAL, 0,1,0,100); //Checkbox check1 = new Checkbox("check 1"); //Checkbox check2 = new Checkbox("check 2"); //CheckboxGroup cbGroup = new CheckboxGroup(); //Checkbox check3 = new Checkbox("check 3",cbGroup,true); //Checkbox check4 = new Checkbox("check 4",cbGroup,false); //Choice selName = new Choice(); //Font largeFont = new Font("Courier",Font.BOLD,20); public void init(){ //will be part of any applet. Happens when applet first loads //Use this method to set up screen //Create a gridbag layout called myLayout GridBagLayout myLayout = new GridBagLayout(); //create a gridbag constraints object called myConstraints GridBagConstraints myConstraints = new GridBagConstraints(); //bind myLayout to the applet setLayout(myLayout); //change the background color of the applet setBackground(Color.yellow); //set values of constraints for first component //========================================================= myConstraints.gridx = 0; //tells which column component goes into myConstraints.gridy = 0; //tells which row component goes into myConstraints.gridheight = 1; //height of component in cells myConstraints.gridwidth = 1; //width of component in cells myConstraints.fill=GridBagConstraints.HORIZONTAL; //Forces component to expand to given width //can alsu use .HEIGHT and .BOTH myConstraints.weightx = 1; //used in resizing. myConstraints.weighty =1; // "" //bind a component you created above to this set of constraints myLayout.setConstraints(componentName, myConstraints); //add the component to the applet add(componentName); //========================================================= //for each component, copy the myConstraints code and modify as needed. //The only constraints that MUST be different are gridx and gridy //others can be left as defaults //after setting constraints, bind the component to the constraints, and add it. }// end init public boolean handleEvent(Event e){ //Keeps track of what the user has done //NOTE: uses javac version 1.0.2 event handler... //This is great for simple event handling. More complex stuff //(like scroll bars and such) will require the action method. //Check to see which event was activated if (e.target == firstComponent){ //if the object named {firstComponent} was activated... //code you want to occur when this thing has been clicked return true; //you MUST tell the browser we succeeded }else if (e.target == secondComponent){ //check to see if another component was clicked //code for second component activation return true; //again, this is necessary //repeat this for each component you want to test }else{ //if none of these components were activated... return false; //tell the browser nothing happened } // end if // a return statement must be the last code in this // procedure }//end handleEvent //other classes you may want to use //un-comment them if you intend to use them. //public void start(){ //put code here that should happen every time the applet gets the focus //eg browser goes to background and is reactivated //}//end start //public void stop(){ //put code here that should happen each time the applet loses the focus //eg browser goes to background //turn off music when browser is not active or page has been changed. //} //public void paint(Graphics g){ //use graphics methods to paint directly on Applet. //eg: // g.drawString("String to draw",x,y); //draws string at (x,y) coordinates of applet // g.drawRect(x,y,width,height); //draws rectangle at (x,y) with approp. width and ht. // and so on... look up the Graphics object for more; //} }// end class definition