这是Swing中的问题,
你不能直接对JApplet/JFrame/JDialog向Applet/Frame/Dialog那样用add()改成
Container c=getContentPane();
c.add(...);
c.add(...);....

解决方案 »

  1.   

    在main()里好像应该
    JFrame frame=....
    JApplet applet=....
    frame.getContentPane().add(applet);
    而不是在applet里加frame
      

  2.   

    应该是applet加到frame中吧,你看这段:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.applet.*;public class applet5 extends Applet {
    Image img1=null;
    Image img2=null;
    int width1=0,height1=0,w1=0,h1=0,wid1=0,hei1=0,w11=0,h11=0;
    int width2=0,height2=0,w2=0,h2=0,wid2=0,hei2=0,w22=0,h22=0;
    canvas can1=null;
    canvas can2=null;
    MediaTracker mt=new MediaTracker(this); public void init() {
            img1=this.getImage(this.getCodeBase(),"1.gif");
    img2=this.getImage(this.getCodeBase(),"2.gif");
    this.setLayout(null);
    mt.addImage(img1,0);
    mt.addImage(img2,0);
    try {
       mt.waitForAll();
    } catch(Exception ex) {
    System.err.println(ex.toString());
    }
    can1=new canvas(img1);
    can2=new canvas(img2);
    width1=img1.getWidth(this);
    height1=img1.getHeight(this);
    width2=img2.getWidth(this);
    height2=img2.getHeight(this);
    wid1=hei1=100;
    wid2=hei2=200;
    this.add(can1);
    this.add(can2);
    can1.setBounds(60,60,width1,height1);
    can2.setBounds(250,200,width2,height2);
    }    public void dd(){
         /*
         img1=this.getImage(this.getCodeBase(),"1.gif");
    img2=this.getImage(this.getCodeBase(),"2.gif");
    this.setLayout(null);
    mt.addImage(img1,0);
    mt.addImage(img2,0);
    try {
       mt.waitForAll();
    } catch(Exception ex) {
    System.err.println(ex.toString());
    }
    can1=new canvas(img1);
    can2=new canvas(img2);
    width1=img1.getWidth(this);
    height1=img1.getHeight(this);
    width2=img2.getWidth(this);
    height2=img2.getHeight(this);
    wid1=hei1=100;
    wid2=hei2=200;
    this.add(can1);
    this.add(can2);
    can1.setBounds(100,100,width1,height1);
    can2.setBounds(200,200,width2,height2);
    */
        } public void paint(Graphics g) {
    //g.drawRect(200,100,50,50);
    //g.drawRoundRect(200,200,50,50,50,50);
    //Rectangle clipRect = g.getClipBounds();

    //g.drawLine(wid1+width1/2,hei1+height1/2,wid2+width2/2,hei2+height2/2); 
    } class canvas extends Canvas {
    Image im1=null;
    public canvas(Image im) {
    super();
    this.setCursor(new Cursor(Cursor.HAND_CURSOR));
    im1=im;
    this.addMouseMotionListener(new MouseMotionAdapter() {
       public void mouseDragged(MouseEvent e) {
         if(e.getSource()==can1) {
         w1=e.getX();
         h1=e.getY();
         //System.out.println(w1);
         wid1=((applet5)((canvas)e.getSource()).getParent()).can1.getX();
         hei1=((applet5)((canvas)e.getSource()).getParent()).can1.getY();
         //System.out.println(Math.abs(wid1-w1));
         //System.out.println(Math.abs(hei1-h1));
         //if (Math.abs(wid1-w1)<10 && Math.abs(hei1-h1)<10){
         //return;
         ((applet5)((canvas)e.getSource()).getParent()).can1.setBounds(wid1,hei1,width1+w1,height1+h1);//wid1+w1-w11,hei1+h1-h11,width1,height1
         ((applet5)((canvas)e.getSource()).getParent()).validate();
         ((canvas)e.getSource()).repaint();
         ((applet5)((canvas)e.getSource()).getParent()).repaint();
             //}else{
             //((applet5)((canvas)e.getSource()).getParent()).can1.setBounds(wid1+w1-w11,hei1+h1-h11,width1,height1);//wid1+w1-w11,hei1+h1-h11,width1,height1
         //((applet5)((canvas)e.getSource()).getParent()).validate();
         //((canvas)e.getSource()).repaint();
         //((applet5)((canvas)e.getSource()).getParent()).repaint();
            // }
         } else if(e.getSource()==can2) {
           w2=e.getX();
           h2=e.getY();
           wid2=((applet5)((canvas)e.getSource()).getParent()).can2.getX();
           hei2=((applet5)((canvas)e.getSource()).getParent()).can2.getY();
           ((applet5)((canvas)e.getSource()).getParent()).can2.setBounds(wid2+w2-w22,hei2+h2-h22,width2,height2);
           ((applet5)((canvas)e.getSource()).getParent()).validate();
           ((canvas)e.getSource()).repaint();
         ((applet5)((canvas)e.getSource()).getParent()).repaint();
         }
       }
    });
    this.addMouseListener(new MouseAdapter() {
       public void mousePressed(MouseEvent e) {
         if(e.getSource()==can1) {
           w11=e.getX();
           h11=e.getY();
         } else if(e.getSource()==can2) {
           w22=e.getX();
           h22=e.getY();
         }
       }
    });
    } public void paint(Graphics g) {
    g.drawImage(im1,0,0,this);
    }
    }
    /*
    public static void main(String[] args){
        JApplet applet51 = new applet5();
        JFrame frame = new JFrame("Applet1c");
    frame.getContentPane().add(applet51);
    frame.setSize(500,500);
    applet51.init();
    applet51.start();
    frame.setVisible(true);
    }*/
    }
    /**/
      

  3.   

    修改了一点点东东。在本机上测试通过//RadioBoxNew.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class RadioBoxNew extends JApplet {
    JTextField t = new JTextField();
    JCheckBox[] cb = {
    new JCheckBox("CheckBox 1"),
    new JCheckBox("CheckBox 2"),
    new JCheckBox("CheckBox 3")
        };
        ButtonGroup group = new ButtonGroup();
        JRadioButton
          cb4 = new JRadioButton("four"),
          cb5 = new JRadioButton("five"),
          cb6 = new JRadioButton("six");
        //checking the source:
        class ILCheck implements ItemListener{
         public void itemStateChanged(ItemEvent e){
         for(int i = 0;i<cb.length;i++){
         if(e.getSource().equals(cb[i])){
         t.setText("Check Box"+(i+1));
         return;
         }
         }
         }
        }
        // vs. an individual for each class
        class IL4 implements ItemListener{
         public void itemStateChanged(ItemEvent e){
         t.setText("Check Box 4");
         }
        }
        class IL5 implements ItemListener{
         public void itemStateChanged(ItemEvent e){
         t.setText("Check Box 5");
         }
        }
        class IL6 implements ItemListener{
         public void itemStateChanged(ItemEvent e){
         t.setText("Check Box 6");
         }
        }
        class WClose extends WindowAdapter{
         public void windowClosing(WindowEvent e){
         System.exit(0);
         }
        }
        public void init(){
         Container cp = getContentPane();
         cp.setLayout(new FlowLayout());
         t.setEditable(false);
         cp.add(t);
         ILCheck il = new ILCheck();
         for(int i=0;i<cb.length;i++){
         cb[i].addItemListener(il);
         cp.add(cb[i]);
         }
         group.add(cb4);
         group.add(cb5);
         group.add(cb6);
         cb4.addItemListener(new IL4());
         cb5.addItemListener(new IL5());
         cb6.addItemListener(new IL6());
         cp.add(cb4);
         cp.add(cb6);
         cp.add(cb5);
        }
        public static void main(String args[]){
         /*
         JApplet applet = new RadioBoxNew();
         JFrame frame = new JFrame("RadioBoxNew");
         //frame.setDefaultCloseOperation(3);
         frame.addWindowListener(new WClose());
         frame.setSize(300,300);
         applet.add(frame);
         applet.init();
         applet.start();
         frame.setVisible(true);
         */
           //修改处:
         RadioBoxNew applet = new RadioBoxNew();
          JFrame frame = new JFrame("TextAreaNew");
          frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
              System.exit(0);
            }
          });
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          frame.setSize(300,300);
          applet.init();
          applet.start();
          frame.setVisible(true);
        }
    }
      

  4.   

    谢谢 shine333(shine)、 yzxasd(风)、tangshancheng(98007)
    正在调试中......