Applet如何添加事件响应,需要用到什么方法?刚接触JAVA,不太明白,各位哥哥姐姐出来帮帮忙啊,最好有详细的代码,先谢了~

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Example13 extends JApplet{
    JButton b; public void init(){
    b = new JButton("按钮");
    add("Center",b);
    b.addActionListener(new MyListener());
    } public void paint(Graphics g){} class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    if(e.getSource() == b)
    JOptionPane.showMessageDialog(null,"dddddddddd");
    }
    }
    }
      

  2.   

    怪了,那我这段代码错哪了?import java.awt.*;
    import javax.swing.*;
    import java.applet.*;
    import java.awt.event.*;
    public class  click_me extends Applet
    {
    JButton b;
    public void init(){

    b=new JButton("点我");
    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    b=new JButton("按钮一");

    }
    });
    setLayout(new FlowLayout());
    add(b);
    }}我想实现点击按钮后按钮文本有"点我"变为"按钮1",但点后就是没反应!
      

  3.   

    public void init()
        {        b = new JButton("点我");
            b.addActionListener(new ActionListener()
            {            public void actionPerformed(ActionEvent e)
                {
                    b.setText("Test");
                }
            });
            setLayout(new FlowLayout());
            add(b);
        }
      

  4.   

    晕了,原来是方法搞错了,那我用 b=new JButton("按钮一");重新初始化b为什么不行呢??