import javax.swing.*;
import java.awt.*;
import java.awt.event.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class mee {
  public static void main(String[] args){
  JFrame childfr=new  myJFrame();
  childfr.setSize(300,400);
  childfr.show();
 }static class  myJFrame extends JFrame   implements ActionListener {
  public  myJFrame ()
  {
    super();
  Container  con=getContentPane();
  JPanel pan=new JPanel();
  JButton bt=new JButton("关闭窗口");
  bt.addActionListener(this);
  pan.add(bt);
  con.add(pan);  }   public void actionPerformed(ActionEvent e){     System.exit(-1);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
 }
}

解决方案 »

  1.   

    我的意思是说
    在一个已有的窗口里new一个新的frame
    再如何监听新的frame上的Button的动作呢?
    比如点了新frame上的按钮然后就关闭新的哪个frame而不是整个程序
      

  2.   

    应该一样处理吧?
    button在哪个窗口上,没有关系的。
    都可以用button.addActionLisener(new ActionListner(){
       public void actionPerformed(ActionEvent e){
            //code
    }
    };
      

  3.   

    但是如果是这种情况呢?
    class childframe extends JPanel{
    private JButton bt=new JButton("close window");
    public childframe(){
    add(bt);
    }
    }
    class mainframe extends JFame{
    public static void main(String[] args)
    {
    javax.swing.JFrame frame = new javax.swing.JFrame();
    childframe aUI=new childframe();
    frame.setContentPane(aUI);
    frame.show();
    }
    }问题在于bt在那边定义的,怎么加listener呢?