Wuziqimain(){
this.setSize(408,386)
this.setVisible(true)
this.setResizable(false)
this.setLocation(350, 150)
this.addWindowListener(this)
}
public static void main(String[] args) {
new Wuziqimain()
}
//窗口的关闭事件
public void windowClosing(WindowEvent e) {
// TODO 自动生成方法存根
int resturn= JOptionPane.showConfirmDialog(this,
" 你确实要退出游戏吗?",
" 确 认", JOptionPane.YES_NO_OPTION)
if(resturn==JOptionPane.YES_OPTION) //返回的值
System.exit(0)
}
public void windowActivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowClosed(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowOpened(WindowEvent e) {
// TODO 自动生成方法存根
}
}
方法一:初始化的时候添加一个事件监听addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0)
}
})
方法二:建议使用JFrame
因为JFrame可以直接设置关闭时候的默认 *** 作
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE)
系统不关闭,只是隐藏窗体!在按钮的监听事件中获取到窗体,然后设置窗体的visible属性为false既可以例如:
public class window {
public static void main(String[] arg0){
myWindow mw=new myWindow()
mw.setVisible(true)
}
}
class myWindow extends JFrame{
private JButton jb=null
public myWindow() {
jb=new JButton("test")
jb.addActionListener(new mylistener(this))
this.add(jb)
}
}
class mylistener implements ActionListener{
private myWindow mw=null
public mylistener(myWindow mw){
this.mw=mw
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
mw.setVisible(false)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)