将actiongPerformed函数内容改为:
if(e.getActionCommand()=="1")
          JOptionPane.showMessageDialog(this,"第一个按钮");
即可

解决方案 »

  1.   

    你这个问题是因为你的Button b1=new Button("1");是在Medialog的构造函数中定义,而你的监听器里的e.getSource()==b1,b1是在Medialog这个大类中,所以他将只会在Medialog中寻找关于b1的定义,不会去构造函数里找,所以它会提示你b1没有定义!应该这样改一下package myprojects.medialog;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Medialog extends Frame implements ActionListener{
    Button b1=new Button("1");
    Button b2=new Button("2");
    Button b3=new Button("3");
    Panel p=new Panel();

    public Medialog() {
                      b1.addActionListener(this);
           add(p);
    p.add(b1);  
    p.add(b2);
    p.add(b3); 

    } public static void main(String args[]) {
    System.out.println("Starting Medialog...");
    Medialog mainFrame = new Medialog();
    mainFrame.setSize(400, 400);
    mainFrame.setTitle("Medialog");
    mainFrame.setVisible(true);

    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b1)
         JOptionPane.showMessageDialog(this,"第一个按钮");

        
    }
    }本人也是个初学者,所以讲的也不知道对不对,望不要见笑!你可以加我QQ,我们以后有什么问题可以讨论一下! 28658714     
      

  2.   

    你的b1变量定义事不是有问题?你的b1应该只能在Medialog用吧,在方法actiongPerformed是用应该有错误的