Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java에서 WindowListener 인터페이스의 중요성은 무엇입니까?


WindowEvent 를 처리하는 클래스 이 인터페이스를 구현해야 하며 이 클래스의 개체는 addWindowListener()를 사용하여 구성 요소에 등록할 수 있습니다. 방법.

WindowListener 인터페이스의 메소드

창 수신기 인터페이스는 창 이벤트를 처리하는 7가지 방법을 정의합니다.

  • 무효 windowActivated(WindowEvent 우리) − 창이 활성화될 때 호출됩니다.
  • void windowDeactivated(WindowEvent 우리는 ) - 창이 비활성화될 때 호출됩니다.
  • void windowOpened(WindowEvent we) − 창이 열릴 때 호출됩니다.
  • 무효 windowClosed(WindowEvent 우리) − 창이 닫힐 때 호출됩니다.
  • 무효 windowClosing(우리는 WindowEvent) − 창이 닫힐 때 호출됩니다.
  • 무효 windowIconified(WindowEvent 우리) − 창이 최소화될 때 호출됩니다.
  • 무효 windowDeiconfied(WindowEvent 우리) − 창이 복원될 때 호출됩니다.

구문

public interface WindowListener extends EventListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowListenerTest extends JFrame implements WindowListener {
   JLabel l1,l2;
   JTextField t1;
   JPasswordField p1;
   JButton b1;
   public WindowListenerTest() {
      super("WindowListener Test");
      setLayout(new GridLayout(3,2));
      l1 = new JLabel("Name");
      l2 = new JLabel("Password");
      t1 = new JTextField(10);
      p1 = new JPasswordField(10);
      b1 = new JButton("Send");
      add(l1);
      add(t1);
      add(l2);
      add(p1);
      add(b1);
      addWindowListener(this);
   }
   public static void main(String args[]) {
      WindowListenerTest wlt = new WindowListenerTest();
      wlt.setSize(375, 250);
      wlt.setResizable(false);
      wlt.setLocationRelativeTo(null);
      wlt.setVisible(true);
   }
   public void windowClosing(WindowEvent we) {
      this.setVisible(false);
      System.exit(0);
   }
   public void windowActivated(WindowEvent we) {
   }
   public void windowDeactivated(WindowEvent we) {
   }
   public void windowOpened(WindowEvent we) {
   }
   public void windowClosed(WindowEvent we) {
   }
   public void windowIconified(WindowEvent we) {
   }
   public void windowDeiconified(WindowEvent we) {
   }
}

출력

Java에서 WindowListener 인터페이스의 중요성은 무엇입니까?