?? windowlistenerframe.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 + -