很久沒有用過界面編程了,就當復習一下了,哈哈
成都創新互聯公司專業為企業提供沙依巴克網站建設、沙依巴克做網站、沙依巴克網站設計、沙依巴克網站制作等企業網站建設、網頁設計與制作、沙依巴克企業網站模板建站服務,十余年沙依巴克做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
如一樓所說的,給按鈕加一個監聽器ActionListener,寫一個實現方法
actionPerformed.此時當按鈕點擊時會調用actionPerformed方法,代碼如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Close extends JFrame implements ActionListener{
JButton close;
public Close(){
close = new JButton("close");//增加一個按鈕
add(close);
close.addActionListener(this);//給按鈕增加一個監聽器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按鈕點擊時的事件處理方法
//按鈕點擊時一定會自動執行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//關閉整個應用程序.如果只是是想關閉當前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
你用的 swing 嗎?加上 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
或者加上窗口事件監聽器:
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent we) {
dispose();
}
});
前段時間集中精力寫了兩篇論文 很久沒寫博文了 現在繼續了
使用JFrame的enableEvents和processWindowEvent
//Frame java
import java awt *;
import java awt event *;
import javax swing *;
public class Frame extends JFrame {
public Frame () {
enableEvents(AWTEvent WINDOW_EVENT_MASK);
this setSize(new Dimension( ));
this setTitle( Frame );
}
protected void processWindowEvent(WindowEvent e) {
super processWindowEvent(e);
if (e getID() == WindowEvent WINDOW_CLOSING) {
System exit( );
}
}
}
直接實現WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extends Frame implements WindowListener {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(this);
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
public void windowOpened(WindowEvent windowEvent) {? }
public void windowClosed(WindowEvent windowEvent) {? }
public void windowIconified(WindowEvent windowEvent) {? }
public void windowDeiconified(WindowEvent windowEvent) {? }
public void windowActivated(WindowEvent windowEvent) {? }
public void windowDeactivated(WindowEvent windowEvent) {? }
}
直接繼承窗體適配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extends? WindowAdapter {
public Frame () {
Frame f=new Frame();
f setSize(new Dimension( ));
f setTitle( Frame );
f addWindowListener(this);
f setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
間接繼承窗體適配器WindowAdapter
//Frame java
import java awt *;
import java awt event *;
public class Frame extends? Frame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winAdapter());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winAdapter extends WindowAdapter{
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
}
間接實現WindowListener接口
//Frame java
import java awt *;
import java awt event *;
public class Frame extends? Frame {
public Frame () {
this setSize(new Dimension( ));
this setTitle( Frame );
this addWindowListener(new winEventHandle());
this setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
class winEventHandle implements WindowListener {
public void windowClosing(WindowEvent windowEvent) {
System exit( );
}
public void windowOpened(WindowEvent windowEvent) {? }
public void windowClosed(WindowEvent windowEvent) {? }
public void windowIconified(WindowEvent windowEvent) {? }
public void windowDeiconified(WindowEvent windowEvent) {? }
public void windowActivated(WindowEvent windowEvent) {? }
public void windowDeactivated(WindowEvent windowEvent) {? }
}
使用Inner Class
//Frame java
import java awt *;
import java awt event *;
public class Frame {
public Frame (){
Frame f=new Frame();
f addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System exit( );
}
});
f setSize(new Dimension( ));
f setVisible(true);
}
public static void main(String[] s){
new Frame ();
}
}
Jframe的關閉方法
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的關閉方法如下
this addWindowListener(new java awt event WindowAdapter() {
public void windowClosing(java awt event WindowEvent e) {
System exit( );
}
lishixinzhi/Article/program/Java/hx/201311/27073
文章題目:窗體關閉代碼java 永遠關不掉的窗口代碼
文章路徑:http://m.kartarina.com/article16/dodspgg.html
成都網站建設公司_創新互聯,為您提供定制網站、網站維護、網站設計、云服務器、企業網站制作、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯