我建立了两个Frame类 我希望在Frame1中创建一个函数来监听Frame上的一个jbutton触发的事件 能够实现吗? 具体该怎么做? 谢谢

解决方案 »

  1.   

       让那个窗口类实现ActionListener接口,在给那个Button增加addAction....(具体的方法名不记得了,自己根据前面给的那个接口去查查吧
      

  2.   

    import java.awt.event.ActionEvent;    
    import java.awt.event.ActionListener;    
    import javax.swing.JButton;    
      
    public class TestActionListener1 implements ActionListener {    
      
        public void actionPerformed(ActionEvent e) {  
            String strComm  = e.getActionCommand();//得到按钮的名字
             if("按钮".equals(strComm){
          
                 System.out.println("你点击了按钮"); 
            }  
        }    
      
        public void test() {    
            JButton s_button = new JButton("按钮");    
            s_button.addActionListener(this);    
        }    
    }   
      

  3.   

    import java.awt.event.ActionEvent;    
    import java.awt.event.ActionListener;    
    import javax.swing.JButton;    
      
    public class TestActionListener1 implements ActionListener {    
      
        public void actionPerformed(ActionEvent e) {  
            String strComm  = e.getActionCommand();//得到按钮的名字
             if("按钮".equals(strComm){
          
                 System.out.println("你点击了按钮"); 
            }  
        }    
      
        public void test() {    
            JButton s_button = new JButton("按钮");    
            s_button.addActionListener(this);    
        }    
    }
      

  4.   

    不好意思我的问题有点毛病哈  我的意思是在frame1这个类上写一个函数来监听frame2这个类上的一个事件
    比如点击frame2上的一个jbutton 然后frame1上这个监听的函数就会被调用 不是监听this上的事件 是监听同一个package类的另外一个类的事件 可以办到吗?