熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java關閉窗體的六種方法

2022-06-13   來源: Java核心技術 

  前段時間集中精力寫了兩篇論文很久沒寫博文了現在繼續了

  使用JFrame的enableEvents和processWindowEvent

  //Framejava

  import javaawt*;

  import javaawtevent*;

  import javaxswing*;

  public class Frame extends JFrame {

  public Frame() {

  enableEvents(AWTEventWINDOW_EVENT_MASK);

  thissetSize(new Dimension( ));

  thissetTitle(Frame);

  }

  protected void processWindowEvent(WindowEvent e) {

  superprocessWindowEvent(e);

  if (egetID() == WindowEventWINDOW_CLOSING) {

  Systemexit();

  }

  }

  }

  直接實現WindowListener接口

  //Framejava

  import javaawt*;

  import javaawtevent*;

  public class Frame extends Frame implements WindowListener {

  public Frame() {

  thissetSize(new Dimension( ));

  thissetTitle(Frame);

  thisaddWindowListener(this);

  }

  public void windowClosing(WindowEvent windowEvent) {

  Systemexit();

  }

  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

  //Framejava

  import javaawt*;

  import javaawtevent*;

  public class Frame extends  WindowAdapter {

  public Frame() {

  Frame f=new Frame();

  fsetSize(new Dimension( ));

  fsetTitle(Frame);

  faddWindowListener(this);

  fsetVisible(true);

  }

  public static void main(String[] s){

  new Frame();

  }

  public void windowClosing(WindowEvent windowEvent) {

  Systemexit();

  }

  }

  間接繼承窗體適配器WindowAdapter

  //Framejava

  import javaawt*;

  import javaawtevent*;

  public class Frame extends  Frame {

  public Frame() {

  thissetSize(new Dimension( ));

  thissetTitle(Frame);

  thisaddWindowListener(new winAdapter());

  thissetVisible(true);

  }

  public static void main(String[] s){

  new Frame();

  }

  }

  class winAdapter extends WindowAdapter{

  public void windowClosing(WindowEvent windowEvent) {

  Systemexit();

  }

  }

  間接實現WindowListener接口

  //Framejava

  import javaawt*;

  import javaawtevent*;

  public class Frame extends  Frame {

  public Frame() {

  thissetSize(new Dimension( ));

  thissetTitle(Frame);

  thisaddWindowListener(new winEventHandle());

  thissetVisible(true);

  }

  public static void main(String[] s){

  new Frame();

  }

  }

  class winEventHandle implements WindowListener {

  public void windowClosing(WindowEvent windowEvent) {

  Systemexit();

  }

  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

  //Framejava

  import javaawt*;

  import javaawtevent*;

  public class Frame{

  public Frame(){

  Frame f=new Frame();

  faddWindowListener(new WindowAdapter(){

  public void windowClosing(WindowEvent e){

  Systemexit();

  }

  });

  fsetSize(new Dimension( ));

  fsetVisible(true);

  }

  public static void main(String[] s){

  new Frame();

  }

  }

  Jframe的關閉方法

  setDefaultCloseOperation(EXIT_ON_CLOSE);

  frame的關閉方法如下

  thisaddWindowListener(new javaawteventWindowAdapter() {

  public void windowClosing(javaawteventWindowEvent e) {

  Systemexit();

  }

  });


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27073.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.