按钮能发生windowEvent吗,如果不能,我怎么设置一个按钮,当我点击时关闭窗口。
最好给个实例。谢谢!

解决方案 »

  1.   


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;public class DD extends JFrame
    {
        private static final long serialVersionUID = -4158802804225659503L;    public static void main(String args[])
        {
            try
            {
                DD frame = new DD();
                frame.setVisible(true);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }    public DD()
        {
            super();
            getContentPane().setLayout(null);
            setBounds(100, 100, 500, 375);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        final JButton button = new JButton();
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(final ActionEvent e)
                {
                    DD.this.dispose();
                }
            });
            button.setText("Close");
            button.setBounds(120, 168, 106, 28);
            getContentPane().add(button);
        }
    }
      

  2.   

    借楼主的贴顺便也问个问题
    想点击这个按钮后 再打开一个界面  怎么写啊b1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent arg0)
    {

    }

    });
    谁帮我写个例子
      

  3.   

    actionPerformed里加上.
    nf frame = new frame
    {super();
            getContentPane().setLayout(null);
            setBounds(100, 100, 500, 375);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        final JButton button = new JButton();
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(final ActionEvent e)
                {
                    DD.this.dispose();
                }
            });
            button.setText("Close");
            button.setBounds(120, 168, 106, 28);
            getContentPane().add(button);
        }
    }
    }