怎么定义啊?
现在source1就是定义在内部类以外的啊,但是只有在内部类里才可以正确显示,到了外部就是空的了

解决方案 »

  1.   

    我按照你的代码写了个完整的例子(在win2000,jdk1.5环境下编译运行通过测试)
    你看看,可以运行的
    应该对你有点启发import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    class buttonPanel extends JFrame
    {
    String s;
      public buttonPanel()
      {
        JButton IDButton=new JButton("ID");
        JButton NameButton=new JButton("name");
        JButton AgeButton=new JButton("age");
     
        buttonAction ba=new buttonAction();
        IDButton.addActionListener(ba);
        NameButton.addActionListener(ba);
        AgeButton.addActionListener(ba);
        Container con=this.getContentPane();
        con.setLayout(new FlowLayout());
        con.add(IDButton);
        con.add(NameButton);
        con.add(AgeButton);
        
      }  
       private class buttonAction implements ActionListener
      {    public void actionPerformed(ActionEvent e3)
        {
        
      
         if(e3.getActionCommand().equals("ID"))
         {
        
         s="ID";
         System.out.println("您点击的是"+s);
            }
         else if(e3.getActionCommand().equals("name"))
         {
         s="name";
         System.out.println("您点击的是"+s);
         }
         else
         {
         s="age";
         System.out.println("您点击的是"+s);
         }
        }
      
      }
      public static void main(String args[])
      {
       buttonPanel bp=new buttonPanel();
       bp.setSize(300,400);
       bp.show();
      }
     
    }
      

  2.   

    谢谢你的解答,我的程序中如果在actionPerformed里打印的结果也是正确的。但是我是想把这个source1的结果传到其他类里使用,但是在其他类里调用这个source1还是getSource(),返回的结果都是null,怎么才能返回actionPerformed里的source1呢?