Swing WindowAdapter類

2019-10-16 22:11:10

WindowAdapter類是一個用於接收視窗事件的抽象(介面卡)類。此類的所有方法都是空的。此類是用於建立偵聽器物件的便捷類。

類宣告

以下是java.awt.event.WindowAdapter類的宣告 -

public abstract class WindowAdapter
   extends Object
      implements WindowListener, WindowStateListener, WindowFocusListener

類建構函式

編號 建構函式 描述說明
1 WindowAdapter() 無引數建構函式

類方法

編號 類方法 描述說明
1 void windowActivated(WindowEvent e) 啟用視窗時呼叫。
2 void windowClosed(WindowEvent e) 視窗關閉時呼叫。
3 void windowClosing(WindowEvent e) 視窗正處於關閉狀態時呼叫。
4 void windowDeactivated(WindowEvent e) 取消啟用視窗時呼叫。
5 void windowDeiconified(WindowEvent e) 視窗取消圖示化時呼叫。
6 void windowGainedFocus(WindowEvent e) 當視窗設定為焦點視窗時呼叫。
7 void windowIconified(WindowEvent e) 在圖示化視窗時呼叫。
8 void windowLostFocus(WindowEvent e) 當視窗不再是焦點視窗時呼叫。
9 void windowOpened(WindowEvent e) 開啟視窗時呼叫。
10 void windowStateChanged(WindowEvent e) 更改視窗狀態時呼叫。

方法繼承

該類繼承以下類中的方法 -

  • java.lang.Object

WindowAdapter範例

使用編輯器建立以下Java程式:WindowAdapter.java

package com.yiibai.swing.listener;

import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;

public class WindowAdapterDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public WindowAdapterDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
       WindowAdapterDemo  swingAdapterDemo = new WindowAdapterDemo();        
      swingAdapterDemo.showWindowAdapterDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showWindowAdapterDemo(){
      headerLabel.setText("Listener in action: WindowAdapter");      
      JButton okButton = new JButton("OK");
      final JFrame aboutFrame = new JFrame();
      aboutFrame.setSize(300,200);;
      aboutFrame.setTitle("WindowAdapter範例(tw511.com)");

      aboutFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            aboutFrame.dispose();
         }        
      });    
      JLabel msglabel 
         = new JLabel("歡迎來到易百教學/Swing",JLabel.CENTER);
       aboutFrame.add(msglabel);
      aboutFrame.setVisible(true);
   }
}

執行上面範例程式碼,得到以下結果:
WindowAdapter示例