很久沒有用過界面編程了,就當(dāng)復(fù)習(xí)一下了,哈哈
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括曲沃網(wǎng)站建設(shè)、曲沃網(wǎng)站制作、曲沃網(wǎng)頁制作以及曲沃網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,曲沃網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到曲沃省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
如一樓所說的,給按鈕加一個監(jiān)聽器ActionListener,寫一個實(shí)現(xiàn)方法
actionPerformed.此時當(dāng)按鈕點(diǎn)擊時會調(diào)用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);//給按鈕增加一個監(jiān)聽器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按鈕點(diǎn)擊時的事件處理方法
//按鈕點(diǎn)擊時一定會自動執(zhí)行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//關(guān)閉整個應(yīng)用程序.如果只是是想關(guān)閉當(dāng)前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
前段時間集中精力寫了兩篇論文 很久沒寫博文了 現(xiàn)在繼續(xù)了
使用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( );
}
}
}
直接實(shí)現(xiàn)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( );
}
}
間接實(shí)現(xiàn)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的關(guān)閉方法
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame的關(guān)閉方法如下
this addWindowListener(new java awt event WindowAdapter() {
public void windowClosing(java awt event WindowEvent e) {
System exit( );
}
lishixinzhi/Article/program/Java/hx/201311/27073
你用的 swing 嗎?加上 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
或者加上窗口事件監(jiān)聽器:
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent we) {
dispose();
}
});
java關(guān)閉窗口可以使用dispose(),只是該窗體在內(nèi)存中所占有的資源得到了釋放,而整個程序并沒有因此而退出,如果整個程序要退出,在以java.awt中的frame為控件時,需手動添加:System.exit();
而在以javax.Swing中的jFrame為控件時,一般不需要再寫相應(yīng)的事件,默認(rèn)點(diǎn)擊窗體上的小叉時,是隱藏,當(dāng)然你可更改其參數(shù)。
import?java.applet.*;
import?java.awt.Color;
import?java.awt.Frame;
import?javax.swing.JFrame;
import?java.awt.event.*;
public?class?FirstFrame?extends?Frame?{
public?static?void?main(String?args[])?{
FirstFrame?fr?=?new?FirstFrame("First?contianer!");
fr.setSize(240,?240);
//繼承JFrame的關(guān)閉窗口代碼
//fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//繼承Frame的
fr.addWindowListener(new?WindowAdapter()?{????
public?void?windowClosing(WindowEvent?e)?{????????
System.exit(0);//退出系統(tǒng)???
}
});
fr.setVisible(true);
}
public?FirstFrame(String?str)?{
super(str);
}
}
文章題目:java中窗體的關(guān)閉代碼 關(guān)閉窗體的代碼怎么寫
標(biāo)題URL:http://jinyejixie.com/article34/dochgse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、微信小程序、網(wǎng)站改版、用戶體驗(yàn)、域名注冊、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)