⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowlistenerframe.java

📁 Eclipse程序设计经典教程+源代码 学习java的朋友可以看看
💻 JAVA
字号:
package listener;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//应用窗口接收器和动作接收器
public class WindowListenerFrame extends JFrame
    implements ActionListener, WindowListener {

  //声明JPanel容器类
  JPanel contentPane;
  //声明窗口
  JFrame listenerFrame;
  //创建BorderLayout布局
  BorderLayout borderLayout1 = new BorderLayout();
  //创建JTextArea控件
  JTextArea jTextArea1 = new JTextArea();
  //创建JPanel容器类
  JPanel jPanel1 = new JPanel();
  //创建2个按钮
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();

  public static void main(String[] args) {
    //创建本类的窗口类
    WindowListenerFrame frame = new WindowListenerFrame();
    frame.setVisible(true);
  }

  //窗口类构造器
  public WindowListenerFrame() {
    jbInit();
  }
  

  private void jbInit() {
    contentPane = (JPanel)this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("窗口接收器示例1");
    //设置按钮的属性
    jButton1.setText("显示窗口");
    jButton1.setActionCommand("show");
    jButton2.setText("清空内容");
    jButton2.setActionCommand("clear");
    //为按钮加入动作接收器
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);
    //将文本框设为不可编写
    jTextArea1.setEditable(false); 
    //创建带有窗口接收器的窗口
    listenerFrame = new JFrame("窗口事件演示");
    //加入窗口接收器
    listenerFrame.addWindowListener(this); 
    //设置窗口的大小
    listenerFrame.setSize(new Dimension(460, 100)); 
    //为窗口加入标签控件
    JLabel jLabel1 = new JLabel("   窗口接收器,包括打开、关闭、激活、不激活、" 
        + "最小化及恢复显示等事件.");
    listenerFrame.getContentPane().add(jLabel1, BorderLayout.CENTER);

    //为按钮面板加入按钮
    jPanel1.add(jButton1, null);
    jPanel1.add(jButton2, null);
    //为窗口面板加入控件
    contentPane.add(jTextArea1, BorderLayout.CENTER);
    contentPane.add(jPanel1, BorderLayout.SOUTH);
  }

  //当退出主窗口的时,使用System.exit命令清除内存
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  //单击按钮的处理代码
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand() == "show") { 
      //显示带有窗口接收器的窗口
      listenerFrame.setVisible(true);
    }
    else { 
      //清空文本框
      jTextArea1.setText("");
    }
  }

  //窗口正在关闭的处理代码
  public void windowClosing(WindowEvent e) {
    listenerFrame.setVisible(false);
    displayMessage("窗口正在关闭", e);
  }
  //窗口已经关闭的处理代码
  public void windowClosed(WindowEvent e) {
    displayMessage("窗口已经关闭", e);
  }
  //窗口打开的处理代码
  public void windowOpened(WindowEvent e) {
    displayMessage("窗口已经打开", e);
  }
  //窗口图标化的处理代码
  public void windowIconified(WindowEvent e) {
    displayMessage("窗口最小化", e);
  }
  //窗口恢复显示的处理代码
  public void windowDeiconified(WindowEvent e) {
    displayMessage("窗口恢复显示", e);
  }
  //窗口激活的处理代码
  public void windowActivated(WindowEvent e) {
    displayMessage("窗口激活", e);
  }
  //窗口不激活的处理代码
  public void windowDeactivated(WindowEvent e) {
    displayMessage("窗口不激活", e);
  }
  //各种窗口事件的信息显示方法
  void displayMessage(String prefix, WindowEvent e) {
    jTextArea1.append(prefix + "\n");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -