import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Test extends Applet
{ Label lbret = new Label();
Button btn = new Button("Test Button");
int y=0;
int x=2;
int a=1;
public Test()
{
this.setLayout(new GridLayout(2,2));

this.add(btn);
this.add(lbret);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
a=a+1;
y = a*x;
lbret.setText("reseult is "+y);
}
});
}
}

解决方案 »

  1.   

    //不知这能否帮上忙.
    //这是一个简单的响应按钮的方法
    //这些书上也有的.多多看书吧.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ButtonAction extends JFrame{
    int y,a=0,x=0;
    public ButtonAction(){
    this.addWindowListener (new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
    }
    });
      
       JPanel panel = new JPanel();
       JButton button = new JButton("action");
       panel.add(button);
          setSize(100,100);
       getContentPane().add(panel); 
          pack(); 
          setVisible(true); 
          
          button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
           a+=10;
     x++;
     y=a*x;
     System.out.println(y);
    }
    });     }

    public static void main(String args[]){
         new ButtonAction().show();
       }
    }