要不这么做, 能通过编译, 但没有结果, WHY?/*
 * @(#)FirstApplet.java 1.0 04/12/08
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class FirstApplet extends Applet 
{
private String s;
private Button button;
boolean y;

public void init() 
{// Applet的大小在 html中调整
Button button = new Button("Hello");
button.addActionListener(new Handle());
this.add(button);  // this -> Applet
s = "Welcome to Java!!";
} public void paint(Graphics g) 
{
g.drawString(s, 50, 60 );
if (y)      
button.setLabel("World");  
else 
button.setLabel("Java"); 
}

private class Handle implements ActionListener
{
     public void actionPerformed(ActionEvent e)
     {    
     s = "Hello World!";
     y = (!y);
     repaint();  // 重新绘制 Applet 组件
    }
    }

}

解决方案 »

  1.   

    你的这个问题是不能解决Applet更改标签的问题的
    你想用s来绘制标签只有用JApplet来做,这样是可以的
    用JApplet
      

  2.   

    我的目的是要对Button的标签进行修改, 不能实现吗?
      

  3.   

    可以的呀?
    要传对象过来哦!
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;public class FirstApplet extends Applet 
    {
    private String s;
    private Button button;

    public void init() 
    {// Applet的大小在 html中调整
    Button button = new Button("Hello");
    button.addActionListener(new Handle(this));
    this.add(button);  // this -> Applet
    s = "Welcome to Java!!";
    } public void paint(Graphics g) 
    {
    g.drawString(s, 50, 60 );
    }

    private class Handle implements ActionListener
    {           public Handle(FirstApplet you)
               {
                  this.you=you;
                }
         public void actionPerformed(ActionEvent e)
         {    
         s = "Hello World!";
         button.setLabel("World");  // 我这么设置, 错在哪里??
         repaint();  // 重新绘制 Applet 组件
        }
        }

    }
      

  4.   

    private class Handle implements ActionListener
    {
                FirstApplet you;
               public Handle(FirstApplet you)
               {
                  this.you=you;
                }
         public void actionPerformed(ActionEvent e)
         {    
         s = "Hello World!";
         you.button.setLabel("World");  // 我这么设置, 错在哪里??
         repaint();  // 重新绘制 Applet 组件
        }
        }

    }
      

  5.   

    而且你也没必要定义个新类,直接用actionPerformed(ActionEvent e)就行,要是定义新类,应把性类放到FirstApplet类外,class Handle implements ActionListener{ Applet myApple;
    Handle(Applet p){myApplet = p;}
    ……},FirstApplet类中Button.addActionListener(new Handle(this));