新聞中心
怎么用java寫代碼實現(xiàn)在點擊退出菜單時跳出個對話框提示‘‘確定愿意退出游戲?’’,選擇‘‘是’’
給退出菜單添加監(jiān)聽,響應函數(shù)如下JOptionPane.showConfirmDialog就是彈出一個確認窗口,frame參數(shù)即當前主窗口,將它作為確認窗口的父口,此參數(shù)也可以設null。 frame.dispose()表示釋放此窗口的所有資源并關閉。當然嘍,如果點擊退出菜單之后就退出了整個程序,那也可以直接用System.exit(0)來替代frame.dispose();
創(chuàng)新互聯(lián)公司專注于巧家網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供巧家營銷型網(wǎng)站建設,巧家網(wǎng)站制作、巧家網(wǎng)頁設計、巧家網(wǎng)站官網(wǎng)定制、微信小程序服務,打造巧家網(wǎng)絡公司原創(chuàng)品牌,更為您提供巧家網(wǎng)站排名全網(wǎng)營銷落地服務。
public?void?actionPerformed(ActionEvent?e)
{
if?(JOptionPane.showConfirmDialog(frame,?"確認退出?")?==?JOptionPane.YES_OPTION)
{
frame.dispose();
}
}
實現(xiàn)界面登陸,退出功能的java代碼怎么寫?
CS結構系統(tǒng)的退出如下:public void init() {\x0d\x0a this.setTitle("用戶登錄界面");\x0d\x0a this.add(createCenterPane());\x0d\x0a this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);\x0d\x0a this.setSize(new Dimension(450, 335));\x0d\x0a this.setLocationRelativeTo(null);\x0d\x0a // this.setVisible(true);\x0d\x0a this.addWindowListener(new WindowAdapter() {\x0d\x0a public void windowClosing(WindowEvent e) {\x0d\x0a int choose = JOptionPane.showConfirmDialog(null, "是否要退出登錄界面?",\x0d\x0a "系統(tǒng)提示:", JOptionPane.YES_NO_OPTION);\x0d\x0a if (choose == JOptionPane.YES_OPTION) {\x0d\x0a System.exit(1);\x0d\x0a }\x0d\x0a }\x0d\x0a });\x0d\x0a }其中this為JFrame對象。BS結構的退出直接用windows.close()方法就行了!
java關閉當前窗口代碼
方法一:
類 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以設置
以下為改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)設置用戶在此窗體上發(fā)起
"close" 時默認執(zhí)行的操作。必須指定以下選項之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定義):不執(zhí)行任何操作;要求程序在已注冊的
WindowListener 對象的 windowClosing 方法中處理該操作。
HIDE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊的 WindowListener
對象后自動隱藏該窗體。
DISPOSE_ON_CLOSE(在 WindowConstants 中定義):調(diào)用任意已注冊 WindowListener
的對象后自動隱藏并釋放該窗體。
EXIT_ON_CLOSE(在 JFrame 中定義):使用 System exit
方法退出應用程序。僅在應用程序中使用。
默認情況下,該值被設置為 HIDE_ON_CLOSE。更改此屬性的值將導致激發(fā)屬性更改事件,其屬性名稱為
"defaultCloseOperation"。
注:當 Java 虛擬機 (VM) 中最后一個可顯示窗口被釋放后,虛擬機可能會終止
要實現(xiàn)你說的,應該采用
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
方法二:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame {
public Test(){
this.setTitle("title");
this.setSize(300,200);
this.setLocation(100,100);
//設置關閉時什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//監(jiān)聽關閉按鈕的點擊操作
this.addWindowListener(new WindowAdapter(){
//new 一個WindowAdapter 類 重寫windowClosing方法
//WindowAdapter是個適配器類 具體看jdk的幫助文檔
public void windowClosing(WindowEvent e) {
//這里寫對話框
if(JOptionPane.showConfirmDialog(null,
"退出","提
示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
名稱欄目:JAVA退出懸浮代碼 java退出函數(shù)
新聞來源:http://www.ef60e0e.cn/article/doigsdp.html