代有Main的Java程序叫Application(应用程序)
而Applet叫小应用程序
他们不一样,所以Applet中不可以加main
但是可以通过一些简单的修改让Applet变成Application
1、将extends Applet的Applet改为Frame
2、将init改为构造函数
3、在构造函数的末尾添加setBounds(x,y,width,height);show;
大致过程是这样的,具体情况还要修改其他,但因程序而不同

解决方案 »

  1.   

    以下是一个例子,可直接编译。
    import java.io.*;
    import java.util.*;class MultiStringMap extends Hashtable {
      public void add(String key, String value) {
        if(!containsKey(key))
          put(key, new Vector());
        ((Vector)get(key)).addElement(value);
      }
      public Vector getVector(String key) {
        if(!containsKey(key)) {
          System.err.println(
            "ERROR: can't find key: " + key);
          System.exit(1);
        }
        return (Vector)get(key);
      }
      public void printValues(PrintStream p) {
        Enumeration k = keys();
        while(k.hasMoreElements()) {
          String oneKey = (String)k.nextElement();
          Vector val = getVector(oneKey);
          for(int i = 0; i < val.size(); i++)
            p.println((String)val.elementAt(i));
        }
      }
    }public class ClassScanner {
      private File path;
      private String[] fileList;
      private Properties classes = new Properties();
      private MultiStringMap 
        classMap = new MultiStringMap(),
        identMap = new MultiStringMap();
      private StreamTokenizer in;
      public ClassScanner() {
        path = new File(".");
        fileList = path.list(new JavaFilter());
        for(int i = 0; i < fileList.length; i++) {
          System.out.println(fileList[i]);
          scanListing(fileList[i]);
        }
      }
      void scanListing(String fname) {
        try {
          in = new StreamTokenizer(
              new BufferedReader(
                new FileReader(fname)));
          // Doesn't seem to work:
          // in.slashStarComments(true);
          // in.slashSlashComments(true);
          in.ordinaryChar('/');
          in.ordinaryChar('.');
          in.wordChars('_', '_');
          in.eolIsSignificant(true);
          while(in.nextToken() != 
                StreamTokenizer.TT_EOF) {
            if(in.ttype == '/')
              eatComments();
            else if(in.ttype == 
                    StreamTokenizer.TT_WORD) {
              if(in.sval.equals("class") || 
                 in.sval.equals("interface")) {
                // Get class name:
                   while(in.nextToken() != 
                         StreamTokenizer.TT_EOF
                         && in.ttype != 
                         StreamTokenizer.TT_WORD)
                     ;
                   classes.put(in.sval, in.sval);
                   classMap.add(fname, in.sval);
              }
              if(in.sval.equals("import") ||
                 in.sval.equals("package"))
                discardLine();
              else // It's an identifier or keyword
                identMap.add(fname, in.sval);
            }
          }
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
      void discardLine() {
        try {
          while(in.nextToken() != 
                StreamTokenizer.TT_EOF
                && in.ttype != 
                StreamTokenizer.TT_EOL)
            ; // Throw away tokens to end of line
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
      // StreamTokenizer's comment removal seemed
      // to be broken. This extracts them:
      void eatComments() {
        try {
          if(in.nextToken() != 
             StreamTokenizer.TT_EOF) {
            if(in.ttype == '/')
              discardLine();
            else if(in.ttype != '*')
              in.pushBack();
            else 
              while(true) {
                if(in.nextToken() == 
                  StreamTokenizer.TT_EOF)
                  break;
                if(in.ttype == '*')
                  if(in.nextToken() != 
                    StreamTokenizer.TT_EOF
                    && in.ttype == '/')
                    break;
              }
          }
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
      public String[] classNames() {
        String[] result = new String[classes.size()];
        Enumeration e = classes.keys();
        int i = 0;
        while(e.hasMoreElements())
          result[i++] = (String)e.nextElement();
        return result;
      }
      public void checkClassNames() {
        Enumeration files = classMap.keys();
        while(files.hasMoreElements()) {
          String file = (String)files.nextElement();
          Vector cls = classMap.getVector(file);
          for(int i = 0; i < cls.size(); i++) {
            String className = 
              (String)cls.elementAt(i);
            if(Character.isLowerCase(
                 className.charAt(0)))
              System.out.println(
                "class capitalization error, file: "
                + file + ", class: " 
                + className);
          }
        }
      }
      public void checkIdentNames() {
        Enumeration files = identMap.keys();
        Vector reportSet = new Vector();
        while(files.hasMoreElements()) {
          String file = (String)files.nextElement();
          Vector ids = identMap.getVector(file);
          for(int i = 0; i < ids.size(); i++) {
            String id = 
              (String)ids.elementAt(i);
            if(!classes.contains(id)) {
              // Ignore identifiers of length 3 or
              // longer that are all uppercase
              // (probably static final values):
              if(id.length() >= 3 &&
                 id.equals(
                   id.toUpperCase()))
                continue;
              // Check to see if first char is upper:
              if(Character.isUpperCase(id.charAt(0))){
                if(reportSet.indexOf(file + id)
                    == -1){ // Not reported yet
                  reportSet.addElement(file + id);
                  System.out.println(
                    "Ident capitalization error in:"
                    + file + ", ident: " + id);
                }
              }
            }
          }
        }
      }
      static final String usage =
        "Usage: \n" + 
        "ClassScanner classnames -a\n" +
        "\tAdds all the class names in this \n" +
        "\tdirectory to the repository file \n" +
        "\tcalled 'classnames'\n" +
        "ClassScanner classnames\n" +
        "\tChecks all the java files in this \n" +
        "\tdirectory for capitalization errors, \n" +
        "\tusing the repository file 'classnames'";
      private static void usage() {
        System.err.println(usage);
        System.exit(1);
      }
      public static void main(String[] args) {
        if(args.length < 1 || args.length > 2)
          usage();
        ClassScanner c = new ClassScanner();
        File old = new File(args[0]);
        if(old.exists()) {
          try {
            // Try to open an existing 
            // properties file:
            InputStream oldlist =
              new BufferedInputStream(
                new FileInputStream(old));
            c.classes.load(oldlist);
            oldlist.close();
          } catch(IOException e) {
            System.err.println("Could not open "
              + old + " for reading");
            System.exit(1);
          }
        }
        if(args.length == 1) {
          c.checkClassNames();
          c.checkIdentNames();
        }
        // Write the class names to a repository:
        if(args.length == 2) {
          if(!args[1].equals("-a"))
            usage();
          try {
            BufferedOutputStream out =
              new BufferedOutputStream(
                new FileOutputStream(args[0]));
            c.classes.save(out,
              "Classes found by ClassScanner.java");
            out.close();
          } catch(IOException e) {
            System.err.println(
              "Could not write " + args[0]);
            System.exit(1);
          }
        }
      }
    }class JavaFilter implements FilenameFilter {
      public boolean accept(File dir, String name) {
        // Strip path information:
        String f = new File(name).getName();
        return f.trim().endsWith(".java");
      }
    } ///:~
      

  2.   

    不好意思,贴错了,这个才是我要给你的例子。
    // Shapes.java
    // Shapes demonstrates some Java 2D shapes.
     
    // Java core packages
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.applet.*;
    public class Shapes extends Applet {   // constructor method
       public Shapes() 
       {
          //super( "Drawing 2D shapes" );
       }   // draw shapes using Java 2D API
       public void paint( Graphics g )
       {
          // call superclass' paint method
          super.paint( g );      // get Graphics 2D by casting g to Graphics2D
          Graphics2D graphics2D = ( Graphics2D ) g;      // draw 2D ellipse filled with blue-yellow gradient
          graphics2D.setPaint( new GradientPaint
             ( 5, 30, Color.blue, 35, 100, Color.yellow, true ) );  
          graphics2D.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) );      // draw 2D rectangle in red
          graphics2D.setPaint( Color.red );                  
          graphics2D.setStroke( new BasicStroke( 10.0f ) ); 
          graphics2D.draw( 
             new Rectangle2D.Double( 80, 30, 65, 100 ) );      // draw 2D rounded rectangle with BufferedImage background
          BufferedImage bufferedImage = new BufferedImage(
             10, 10, BufferedImage.TYPE_INT_RGB );      Graphics2D graphics = bufferedImage.createGraphics();   
          graphics.setColor( Color.yellow ); // draw in yellow
          graphics.fillRect( 0, 0, 10, 10 ); // draw filled rectangle
          graphics.setColor( Color.black );  // draw in black
          graphics.drawRect( 1, 1, 6, 6 );   // draw rectangle
          graphics.setColor( Color.blue );   // draw in blue
          graphics.fillRect( 1, 1, 3, 3 );   // draw filled rectangle
          graphics.setColor( Color.red );    // draw in red
          graphics.fillRect( 4, 4, 3, 3 );   // draw filled rectangle      // paint buffImage into graphics context of JFrame
          graphics2D.setPaint( new TexturePaint(
             bufferedImage, new Rectangle( 10, 10 ) ) );
          graphics2D.fill( new RoundRectangle2D.Double(
             155, 30, 75, 100, 50, 50 ) );      // draw 2D pie-shaped arc in white
          graphics2D.setPaint( Color.white );
          graphics2D.setStroke( new BasicStroke( 6.0f ) ); 
          graphics2D.draw( new Arc2D.Double(
             240, 30, 75, 100, 0, 270, Arc2D.PIE ) );      // draw 2D lines in green and yellow
          graphics2D.setPaint( Color.green );
          graphics2D.draw( new Line2D.Double( 395, 30, 320, 150 ) );      float dashes[] = { 10, 2 };      graphics2D.setPaint( Color.yellow );    
          graphics2D.setStroke( new BasicStroke( 
             4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 
             10, dashes, 0 ) ); 
          graphics2D.draw( new Line2D.Double( 320, 30, 395, 150 ) );
       
       } // end method paint   // start application
       public static void main( String args[] )
       {
        Shapes application=new Shapes();
          Frame frame = new Frame("我爱你");
          frame.addWindowListener(
          new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });      frame.add(application, BorderLayout.CENTER);
          frame.setSize( 425, 260 );
          application.init();
          application.start();
          frame.setVisible( true );
       }
    }  /***************************************************************
     * (C) Copyright 2002 by Deitel & Associates, Inc. and         *
     * Prentice Hall. All Rights Reserved.                         *
     *                                                             *
     * DISCLAIMER: The authors and publisher of this book have     *
     * used their best efforts in preparing the book. These        *
     * efforts include the development, research, and testing of   *
     * the theories and programs to determine their effectiveness. *
     * The authors and publisher make no warranty of any kind,     *
     * expressed or implied, with regard to these programs or to   *
     * the documentation contained in these books. The authors     *
     * and publisher shall not be liable in any event for          *
     * incidental or consequential damages in connection with, or  *
     * arising out of, the furnishing, performance, or use of      *
     * these programs.                                             *
     ***************************************************************/