请前辈评论applet编码风格,还能否优化
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Button extends JApplet {
         private static final long serialVersionUID=123456L

JButton b1 = new JButton("向左走");
JButton b2 = new JButton("向右走");
 
 
 public void init() {
   this.getContentPane();
   run(this);
   this.setVisible(true);
} public  void run(JApplet applet) {

 container();
 JFrame frame = new JFrame(); 
 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
          //响应关闭按钮     } private void container() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
}
 }

解决方案 »

  1.   

    这段程序,在eclipse中运行正常,而在命令行中不报错,但也无显示,在ie中报 Button notinited 错误,请高手帮忙解决.......
      

  2.   

    变量的名字最好用要用b1这些没有明确意义的 写到页面里面用 appletviewer看看吧
      

  3.   

    你的意思是指  init()方法 里的this 最好改为定义过的变量?  好的,谢谢,我先试试....
      

  4.   

    不行啊,我将入口程序加上局部变量还是有问题,在命令行用appletviewer后没有反应,ie里也notinited(绝对不是javaTM的问题,其它都可以正常载入)
    我怀疑我的入口初始化有概念上的错误,一时也想不明白,因为eclipse上运行也正常,请哪位前辈点睛,急啊~~~~~~
    public void init() 
    {   Button objButton =new Button();
        this.setSize(500, 200);
    objButton.getContentPane();
        run(objButton);
        objButton.setVisible(true);
        
    }
      

  5.   

    package myProject;import javax.swing.JApplet;
    import javax.swing.JButton;
    import java.awt.event.*;
    import java.awt.Font;
    import java.awt.Color;
    //import java.util.Vector;
    public class MyProject extends JApplet 

    private static final long serialVersionUID = 234567L ;
        private javax.swing.JPanel jContentPanel = null;
        private MyButton leftButton = null;
        private MyButton rightButton = null; public void init() {
    this.setSize(500,200);
        this.setContentPane(getJContentPanel());  }...........问题解决了,采用这种方法比较好~!!!
      

  6.   

    你那个源程序 明显有问题 ,你参考一下 我写的这个程序你看看好不好 !!!public void saveJPG(Image img, String s) {
    BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
    /*
     * BufferedImage bi = new BufferedImage( img.getWidth(null),
     * img.getHeight(null), BufferedImage.TYPE_INT_RGB);
     */ Graphics2D g2 = bi.createGraphics();
    g2.clipRect(rectX, rectY, rectWidth, rectHeight);
    g2.drawImage(img, null, null);
    int moveX = rectX > 0 ? rectX : 0;
    int moveY = rectY > 0 ? rectY : 0;
    int cutWidth = rectX + rectWidth > imgWidth ? rectWidth
    - ((rectX + rectWidth) - imgWidth) : rectWidth;
    int cutHeight = rectY + rectHeight > imgHeight ? rectHeight
    - ((rectY + rectHeight) - imgHeight) : rectHeight;
    bi = bi.getSubimage(moveX, moveY, cutWidth, cutHeight); FileOutputStream out = null;
    try {
    out = new FileOutputStream(s);
    } catch (java.io.FileNotFoundException io) {
    System.out.println("File   Not   Found");
    }
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(1f, false);
    encoder.setJPEGEncodeParam(param);
    try {
    encoder.encode(bi);
    out.close();
    } catch (java.io.IOException io) {
    System.out.println("IOException");
    }
    }
    }