Ejemplos de las notas


Applets


       import java.awt.Graphics;
       import java.awt.Font;
       import java.awt.Color;
       import java.applet.Applet;

       public class ColorHelloApplet extends Applet {
         Font f = new Font("TimesRoman",Font.BOLD,36);
         public void paint(Graphics g) {
           g.setFont(f);
           g.setColor(Color.red);
           g.drawString("Welcome to Java Programming!!!", 5, 50);
         }
       }

        import java.awt.*;
        import java.applet.*;

        public class ChoiceTest extends Applet {
          Choice colorChoice = new Choice();
          public void init() {
            setBackground(Color.red);
            colorChoice.addItem("Red");
            colorChoice.addItem("Yellow");
            colorChoice.addItem("Green");
            colorChoice.addItem("Blue");
            colorChoice.addItem("White");
            colorChoice.addItem("Black");
            add(colorChoice);
          };
          public boolean action(Event evt, Object arg) {
            if (evt.target == colorChoice) {
              if (((String)arg).equals("Red"))
                setBackground(Color.red);
              else if (((String)arg).equals("Yellow"))
                setBackground(Color.yellow);
              else if (((String)arg).equals("Green"))
                setBackground(Color.green);
              else if (((String)arg).equals("Blue"))
                setBackground(Color.blue);
              else if (((String)arg).equals("White"))
                setBackground(Color.white);
              else
                setBackground(Color.black);
              repaint();
              return true;
            }
            return false; 
          };
        }

       import java.applet.*;
       import java.awt.*;
   
       public class ButtonApplet5 extends Applet {

           Button redButton    = new Button("Red");
           Button yellowButton = new Button("Yellow");
           Button orangeButton = new Button("Orange");
           Button blackButton  = new Button("Black");
           Button blueButton   = new Button("Blue");
           Button whiteButton  = new Button("White");

           Panel colorPanel;
           Panel buttonPanel;

           public void init() {

               setLayout(new GridLayout(2, 1));  // Applet has
                                                 // 2 row, 1 cols

               buttonPanel = new Panel();
               buttonPanel.setLayout(new GridLayout(2, 3));
               buttonPanel.add(redButton);
               buttonPanel.add(yellowButton);
               buttonPanel.add(orangeButton);
               buttonPanel.add(blackButton);
               buttonPanel.add(blueButton);
               buttonPanel.add(whiteButton);

               colorPanel = new Panel();
               colorPanel.setBackground(Color.white);

               add(colorPanel);      // add colorPanel to applet
               add(buttonPanel);     // add buttonPanel to applet
               

           }

           public boolean action(Event evt, Object arg) {
              if (evt.target instanceof Button) {
                 if (evt.target == redButton)
                    colorPanel.setBackground(Color.red);
                 else if (evt.target == yellowButton)
                    colorPanel.setBackground(Color.yellow);
                 else if (evt.target == orangeButton)
                    colorPanel.setBackground(Color.orange);
                 else if (evt.target == blackButton)
                    colorPanel.setBackground(Color.black);
                 else if (evt.target == blueButton)
                    colorPanel.setBackground(Color.blue);
                 else
                    colorPanel.setBackground(Color.white);
                 colorPanel.repaint();
                 return true;
              }
              return false;
           }
       }


        import java.awt.*;
        import java.applet.*;

        public class RadiobuttonTest extends Applet {

          CheckboxGroup cbg = new CheckboxGroup();
          Checkbox small = new Checkbox("Small", cbg, true);
          Checkbox medium = new Checkbox("Medium", cbg, false);
          Checkbox large = new Checkbox("Large", cbg, false);
          Checkbox extraLarge = new Checkbox("Extra Large", cbg, false);
          int fontSize = 10;

          public void init() {

            small.setBackground(Color.yellow);
            medium.setBackground(Color.yellow);
            large.setBackground(Color.yellow);

            add(small);
            add(medium);
            add(large);
            add(extraLarge);
            extraLarge.setBackground(Color.yellow);
            setBackground(Color.yellow);
          }

          public boolean action(Event evt, Object arg) {
            if (evt.target instanceof Checkbox) {
               if (evt.target == small)
                  fontSize = 10;
               else if (evt.target == medium)
                  fontSize = 16;
               else if (evt.target == large)
                  fontSize = 24;
               else
                  fontSize = 30;
               repaint();
               return true;
            }
            return false; 

          }

          public void paint(Graphics g) {

            Font f = new Font("TimesRoman", Font.BOLD, fontSize);
            g.setFont(f);
            g.drawString("I love Radiobuttons!!!!", 5, 95);
          }

        }



       import java.awt.*;
       import java.applet.*;

       public class Keys extends Applet {

         char currkey = 'Q';      // set initial value to Q    
         int currx;
         int curry;

         public void init() {

           currx = size().width / 2;   // start character in center
           curry = size().height / 2;
           setBackground(Color.green);
           setFont(new Font("Helvetica",Font.BOLD,36));
           requestFocus();             // request the input focus
         }

         public boolean mouseDown (Event evt , int  x, int y) {
         curry = y;
         currx = x;
         return true;
         }


         public boolean keyDown(Event evt, int key) {
           switch (key) {
           case Event.DOWN: 
             curry += 5;
             break;
           case Event.UP: 
             curry -= 5;
             break;
           case Event.LEFT:
             currx -= 5;
             break;
           case Event.RIGHT:
             currx += 5;
             break;
           default:
             currkey = (char)key;
           }

           repaint();
           return true;
         }


         public void paint(Graphics g) {
             g.drawString(String.valueOf(currkey), currx,curry);
         }
       }


    import java.awt.*;
    import java.applet.*;

    public class TextTest extends Applet {


       Panel p;
       TextArea ta;
       TextField from;
       TextField to;
       Button replace = new Button("Replace");

       public void init() {

           setLayout(new BorderLayout());

           p = new Panel();
           p.setLayout(new FlowLayout());
           p.add(replace);
           from = new TextField(10);
           p.add(from);
           p.add(new Label("with"));
           to = new TextField(10);
           p.add(to);

           add("South", p);
           ta = new TextArea(8,40);
           add("Center", ta);
       }
   
   
 
       public boolean action(Event evt, Object arg) {
          if (arg.equals("Replace")) {
              String f = from.getText();
              int n = ta.getText().indexOf(f);
              if (n >= 0 && f.length() > 0)
              ta.replaceText(to.getText(), n, n + f.length());
              return true;
          }
          return false;
       }
    }


Modificado el: Mon Jul 12 11:43:06 1999, por: Ramon Reyes Carrion