class Frame{
JButton;
JButton.addActionListener(ActionListener1);
}class ActionListener1{
public void actionPerformed(ActionEvent e){Object obj=e.getSource();
if(obj==new Frame().JButton
){
--------------------
}
}
代码只是把重点写出来了如果将两个类的内容写在同一个类中,访问起来很方便但是现在我想把功能清晰的分出来,写在两个类中在ActionListener1访问的JButton,感觉和Frame中的JButton不是同一个,就是在界面中点了也没反应,其中的代码没有问题,问题应该出在实例化对象中,但具体哪一点错了,我不知道,如果把JButton定义为静态的也能解决问题,就没有其它别的办法了吗访问个组件应该不是件难事吧

解决方案 »

  1.   


    class   Frame{ 
    JButton; 
    JButton.addActionListener(ActionListener1); 
    } class   ActionListener1{ 
    public   void   actionPerformed(ActionEvent   e){ Object   obj=e.getSource(); 
    if(obj==new   Frame().JButton){ 
    -------------------- 

    } LZ :
    Object   obj=e.getSource(); 
    if(obj==new   Frame().JButton){ 
    -------------------- 

    你这个代码是什么东东啊 ,我怎么看不到.. 你用e.getSource()得到的是一个String哦  你
    拿他和一个new Frame().JButton 能这样比吗?
    比如说你有很多按钮
    JButton ok = new JButton("ok");
    JButton cancel = new JButton("cancel");
    你要这个类的监听器 如果你这个类中就只为JButton添加事件,最好是实现ActionListener接口就OK了.
    如果还有其他的事件就要使用内部类,如果事件的是与一个例如与数据库或者文件系统等相关联的话,还需要
    使这个内部类extends Thread使他成为一个单独的线程,我就以简单的实现ActionListener为例吧
    class   MyFrame extends Frame inplements ActionListener{ 
          JButton ok = new JButton("ok");
          JButton cancel = new JButton("cancel"); 
          public MyFrame {
                ok = new JButton("ok");
                cancel = new JButton("cancel");
                ok.addActionListener(thgis); 
                cancel. addActionListener(this);
                .........
                ......... 
          }
          
          public   void   actionPerformed(ActionEvent   e){ 
                if( (e.getSource().trim).equals("ok")){ 
                  // 要操作的内容
                } 
                if( (e.getSource().trim).equals("cancel")){ 
                  // 要操作的内容
                  }      } 
      

  2.   

    分开以后,你的ActionListener就不认识你的Frame了,所以必须把Frame的instance传给你的ActionListener
    class YourFrame extends Frame { 
        JButton btn = new JButton("test"); 
        btn.addActionListener(new ActionListener1(this)); //here
    } class   ActionListener1{ 
        Frame frame;
        public ActionListener1(Frame frame) { //here
            this.frame = frame;
        }
        public   void   actionPerformed(ActionEvent   e){ 
            Object   obj=e.getSource(); 
            if (frame instanceof YourFrame) {
                YourFrame f = (YourFrame)frame;
                if(obj==f.btn){ 
                -------------------- 
                } 
            }
        }

    为什么要ActionListener和Frame分开,难道你想做个通用的ActionListener?
      

  3.   


    不好意思,我上面的方法记错了getSource()是e.getActionCommand()...
    应该是:比如说你有很多按钮
    JButton ok = new JButton("ok");
    JButton cancel = new JButton("cancel");
    你要这个类的监听器 如果你这个类中就只为JButton添加事件,最好是实现ActionListener接口就OK了.
    如果还有其他的事件就要使用内部类,如果事件的是与一个例如与数据库或者文件系统等相关联的话,还需要
    使这个内部类extends Thread使他成为一个单独的线程,我就以简单的实现ActionListener为例吧
    class   MyFrame extends Frame inplements ActionListener{ 
          JButton ok = new JButton("ok");
          JButton cancel = new JButton("cancel"); 
          public MyFrame {
                ok = new JButton("ok");
                cancel = new JButton("cancel");
                ok.addActionListener(thgis); 
                cancel. addActionListener(this);
                .........
                ......... 
          }
          
          public   void   actionPerformed(ActionEvent   e){ 
                if( (e.getActionCommand().trim()).equals("ok")){ 
                  // 要操作的内容
                  } 
                if( (e.getActionCommand().trim()).equals("cancel")){
                }