posts - 41,  comments - 40,  trackbacks - 0

import java.awt.Toolkit;
import java.awt.Dimension;

import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


/*******************************************************************************
 *
 * 关闭前提醒
 *
 * Author: NeedJava
 *
 * Modofied: 2005.04.02
 *
 ******************************************************************************/
class AlarmBeforeClosed extends JFrame
{
  public AlarmBeforeClosed( String title )
  {
    //设置程序标题栏
    this.setTitle( title );

    //不给最大化
    this.setResizable( false );

    //设置程序界面大小
    this.setSize( new Dimension( 500, 400 ) );

    //将程序居中放置(方法1)
    Dimension paneSize=this.getSize();
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation( ( screenSize.width-paneSize.width )/2, ( screenSize.height-paneSize.height )/2 );

    //将程序居中放置(方法2)
    //this.setLocationRelativeTo( null );

    //设置程序关闭的类型,防止关闭
    this.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );

    //设置关闭程序
    this.addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        closeApplication();
      }
    } );
  }


  /*****************************************************************************
   *
   * 关闭程序
   *
   ****************************************************************************/
  private void closeApplication()
  {
    Toolkit.getDefaultToolkit().beep();

    int answer=JOptionPane.showConfirmDialog( AlarmBeforeClosed.this,
                                              "您真的要退出此系统?",
                                              "退出程序",
                                              JOptionPane.YES_NO_OPTION,
                                              JOptionPane.QUESTION_MESSAGE,
                                              null );

    if( answer==JOptionPane.YES_OPTION )  //选择“是”
      {
        System.exit( 0 );
      }
    else if( answer==JOptionPane.NO_OPTION )  //选择“否”
           {
             return;
           }
  }


  /*****************************************************************************
   *
   * 入口主程序
   *
   ****************************************************************************/
  public static void main( String[] args )
  {
    new AlarmBeforeClosed( "关闭前提醒" ).setVisible( true );
  }
}

posted on 2007-09-17 22:56 NeedJava 阅读(1151) 评论(1)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: