1. 程式人生 > >張雲飛 201771010143 《面對物件程式設計(java)》第十三週學習總結

張雲飛 201771010143 《面對物件程式設計(java)》第十三週學習總結

1、實驗目的與要求

(1) 掌握事件處理的基本原理,理解其用途;

(2) 掌握AWT事件模型的工作機制;

(3) 掌握事件處理的基本程式設計模型;

(4) 瞭解GUI介面元件觀感設定方法;

(5) 掌握WindowAdapter類、AbstractAction類的用法;

(6) 掌握GUI程式中滑鼠事件處理技術。

2、實驗內容和步驟

實驗1: 匯入第11章示例程式,測試程式並進行程式碼註釋。

測試程式1:

l  在elipse IDE中除錯執行教材443頁-444頁程式11-1,結合程式執行結果理解程式;

l  在事件處理相關程式碼處添加註釋;

l  用lambda表示式簡化程式;

l  掌握JButton元件的基本API;

l  掌握Java中事件處理的基本程式設計模型。

 

 

package button;

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

/**
* A frame with a button panel
*/
public class ButtonFrame extends JFrame
{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;//寬300
private static final int DEFAULT_HEIGHT = 200;//高200

public ButtonFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// 建立按鈕
JButton orangeButton = new JButton("Orange");//建立一個帶文字的按鈕。
JButton blueButton = new JButton("blue");
JButton greyButton = new JButton("Grey");

buttonPanel = new JPanel();

// 向面板新增按鈕
buttonPanel.add(orangeButton);
buttonPanel.add(blueButton);
buttonPanel.add(greyButton);

// 向框架新增面板
add(buttonPanel);

// 建立按鈕操作
ColorAction orangeAction= new ColorAction(Color.ORANGE);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction greyAction = new ColorAction(Color.GRAY);

// 將操作與按鈕相關聯
orangeButton.addActionListener(orangeAction);
blueButton.addActionListener(blueAction);
greyButton.addActionListener(greyAction);
}

/**
* An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener
{
private Color backgroundColor;

public ColorAction(Color c)
{
backgroundColor = c;
}

public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
}
}

package button;

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

/**
* @version 1.34 2015-06-12
* @author Cay Horstmann
*/
public class ButtonTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new ButtonFrame();
frame.setTitle("ButtonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉按鈕生效
frame.setVisible(true);//介面的可見
});
}
}

 

 

 

 改進後

 

package button;

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

/**
* A frame with a button panel
*/
public class ButtonFrame extends JFrame
{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

public ButtonFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

buttonPanel = new JPanel();


add(buttonPanel);


makeButton("yellow",Color.YELLOW);
makeButton("blue",Color.BLUE);
makeButton("red",Color.RED);
makeButton("green",Color.GREEN);

}
public void makeButton(String name , Color backgroundColor)
{
JButton button=new JButton(name);
buttonPanel.add(button);
ColorAction action=new ColorAction(backgroundColor);
button.addActionListener(action);
}

/**
* An action listener that sets the panel's background color.
*/
private class ColorAction implements ActionListener
{
private Color backgroundColor;

public ColorAction(Color c)
{
backgroundColor = c;
}

public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
}
}

 

 

 

 再度改進:(匿名內部類)

 

 

測試程式2:

l  在elipse IDE中除錯執行教材449頁程式11-2,結合程式執行結果理解程式;

l  在元件觀感設定程式碼處添加註釋;

l  瞭解GUI程式中觀感的設定方法。

 

package button;

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

/**
* A frame with a button panel
*/
public class ButtonFrame extends JFrame
{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

public ButtonFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

buttonPanel = new JPanel();


add(buttonPanel);


makeButton("yellow",Color.YELLOW);
makeButton("blue",Color.BLUE);
makeButton("red",Color.RED);
makeButton("green",Color.GREEN);

}
public void makeButton(String name , Color backgroundColor)
{
JButton button=new JButton(name);
buttonPanel.add(button);
//ColorAction action=new ColorAction(backgroundColor);
//button.addActionListener(action);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
buttonPanel.setBackground(backgroundColor);
}
});
}
}

 

package plaf;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
* A frame with a button panel for changing look-and-feel
*/
public class PlafFrame extends JFrame
{
private JPanel buttonPanel;

public PlafFrame()
{
buttonPanel = new JPanel();

UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
//為了配置選單或為了初始應用程式設定而提供關於已安裝的 LookAndFeel 的少量資訊
for (UIManager.LookAndFeelInfo info : infos)
makeButton(info.getName(), info.getClassName());

add(buttonPanel);
pack();
}

/**
* Makes a button to change the pluggable look-and-feel.
* @param name the button name
* @param className the name of the look-and-feel class
*/
private void makeButton(String name, String className)
{
// 向面板新增按鈕

JButton button = new JButton(name);
buttonPanel.add(button);

//設定按鈕操作

button.addActionListener(event -> {
// 按鈕動作:切換到新的外觀
try
{
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(this);
pack();
}
catch (Exception e)
{
e.printStackTrace();
}
});
}
}

 

 

測試程式3:

l  在elipse IDE中除錯執行教材457頁-458頁程式11-3,結合程式執行結果理解程式;

l  掌握AbstractAction類及其動作物件;

l  掌握GUI程式中按鈕、鍵盤動作對映到動作物件的方法。

package action;

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

/**
* A frame with a panel that demonstrates color change actions.
*/
public class ActionFrame extends JFrame
{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

public ActionFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

buttonPanel = new JPanel();

// 定義的行為
Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),
Color.YELLOW);
Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);

//為這些操作新增按鈕
buttonPanel.add(new JButton(yellowAction));
buttonPanel.add(new JButton(blueAction));
buttonPanel.add(new JButton(redAction));

// 向框架新增面板
add(buttonPanel);

//將Y、B和R鍵與名稱關聯起來
InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

// 將名稱與動作關聯起來
ActionMap amap = buttonPanel.getActionMap();
amap.put("panel.yellow", yellowAction);
amap.put("panel.blue", blueAction);
amap.put("panel.red", redAction);
}

public class ColorAction extends AbstractAction
{
/**
* Constructs a color action.
* @param name the name to show on the button
* @param icon the icon to display on the button
* @param c the background color
*/
public ColorAction(String name, Icon icon, Color c)
{
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
putValue("color", c);
}

public void actionPerformed(ActionEvent event)
{
Color c = (Color) getValue("color");
buttonPanel.setBackground(c);
}
}
}

   

package action;

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

/**
* @version 1.34 2015-06-12
* @author Cay Horstmann
*/
public class ActionTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new ActionFrame();
frame.setTitle("ActionTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}

 

 

測試程式4:

l  在elipse IDE中除錯執行教材462頁程式11-4、11-5,結合程式執行結果理解程式;

l  掌握GUI程式中滑鼠事件處理技術。

 

 

package mouse;

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

/**
* A component with mouse operations for adding and removing squares.
*/
public class MouseComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current; //包含滑鼠游標的正方形

public MouseComponent()
{
squares = new ArrayList<>();
current = null;

addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}

public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;

// 畫出所有方塊
for (Rectangle2D r : squares)
g2.draw(r);
}

/**
* Finds the first square containing a point.
* @param p a point
* @return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
}

/**
* Adds a square to the collection.
* @param p the center of the square
*/
public void add(Point2D p)
{
double x = p.getX();
double y = p.getY();

current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
}

/**
* Removes a square from the collection.
* @param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();
}

private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
//如果游標不在正方形內,則新增一個新的正方形
current = find(event.getPoint());
if (current == null) add(event.getPoint());
}

public void mouseClicked(MouseEvent event)
{
//如果雙擊,則刪除當前方塊
current = find(event.getPoint());
if (current != null && event.getClickCount() >= 2) remove(current);
}
}

private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// 如果滑鼠在內部,則將滑鼠游標設定為十字線
// 一個矩形

if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}

public void mouseDragged(MouseEvent event)
{
if (current != null)
{
int x = event.getX();
int y = event.getY();

//拖動當前矩形,使其居中(x, y)
current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}

 

package mouse;

import javax.swing.*;

/**
* A frame containing a panel for testing mouse operations
*/
public class MouseFrame extends JFrame
{
public MouseFrame()
{
add(new MouseComponent());
pack();
}
}

 

package mouse;

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

/**
* @version 1.34 2015-06-12
* @author Cay Horstmann
*/
public class MouseTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new MouseFrame();
frame.setTitle("MouseTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}

 

 

實驗2:結對程式設計練習

利用班級名單檔案、文字框和按鈕元件,設計一個有如下介面(圖1)的點名器,要求使用者點選開始按鈕後在文字輸入框隨機顯示2017級網路與資訊安全班同學姓名,如圖2所示,點選停止按鈕後,文字輸入框不再變換同學姓名,此同學則是被點到的同學姓名。

package 實驗十三;

 

import java.awt.Color;

import java.awt.Font;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

 

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

 

 

public class 點名器 extends JFrame{

private static final long serialVersionUID = 1L;

JFrame jframe= new JFrame("窗體生成");

JPanel jpanel=null;

JPanel imagePanel = null;

BufferedImage image= null;

JLabel label3 = new JLabel(); 

ImageIcon background = new ImageIcon();

JTextArea jtext = new JTextArea();

    JButton jbutton1=new JButton("開始"); 

    JButton jbutton2=new JButton("暫停");

    JButton jbutton3=new JButton("確定");

    String strPath = "";

    public static boolean flag = true;//判斷開始按鈕是否被點過

    private static Thread t;

    private int count = 0;

    

    public 點名器(){  

    //新增背景圖片

    try {

image=ImageIO.read(new File("F:\\圖片\\95.jpg"));

} catch (IOException e) {

e.printStackTrace();

}

    background = new ImageIcon(image);

    //把背景圖片顯示在一個標籤裡面

JLabel label = new JLabel(background);

//把標籤的大小位置設定為圖片剛好填充整個面板

        label.setBounds(0,0,450,400);

        //把內容窗格轉化為JPanel,否則不能用方法setOpaque()來使內容窗格透明       

        imagePanel = (JPanel)getContentPane();

        imagePanel.setOpaque(false);

        //把背景圖片新增到分層窗格的最底層作為背景       

        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

 

        //新增文字

        jpanel = (JPanel)this.getContentPane();//每次新增必須要加的語句

JLabel label2 = new JLabel("請輸入TXT文字路徑:");

Font font = new Font("",Font.BOLD,30);

label2.setFont(font);

label2.setForeground(Color.yellow);

label2.setBounds(20,50,450,100);

jpanel.add(label2);

        

        //新增按鈕

        jpanel=(JPanel)this.getContentPane();  

        jpanel.setLayout(null);

        //(左,上,寬,高)

        jbutton3.setBounds(new Rectangle(330,180,60,20));

        jbutton3.addActionListener(new TextValue(this));

        jpanel.add(jbutton3);

        

        //新增文字框(左,上,寬,高)

        jtext.setBounds(40, 180, 260, 20);

        jpanel.add(jtext);

        

    }

    /**

     * 重寫構造器

     */

    public 點名器(String str){  

    //將路徑傳入開始按鈕

    strPath = str;

        

    //新增背景圖片

    try {

image=ImageIO.read(new File("F:\\圖片\\95.jpg"));

} catch (IOException e) {

e.printStackTrace();

}

    background = new ImageIcon(image);

    //把背景圖片顯示在一個標籤裡面

JLabel label = new JLabel(background);

//把標籤的大小位置設定為圖片剛好填充整個面板

        label.setBounds(0,0,450,400);

        //把內容窗格轉化為JPanel,否則不能用方法setOpaque()來使內容窗格透明       

        imagePanel = (JPanel)getContentPane();

        imagePanel.setOpaque(false);

        //把背景圖片新增到分層窗格的最底層作為背景       

        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

        

        //新增提示文字

        jpanel = (JPanel)this.getContentPane();//每次新增必須要加的語句

JLabel label2 = new JLabel("點名開始啦!!!");

Font font = new Font("",Font.BOLD,30);

label2.setFont(font);

label2.setForeground(Color.yellow);

label2.setBounds(100,20,450,100);

jpanel.add(label2);

 

//顯示名字資訊

label3.setBounds(150,120,450,100);

    //設定字型顏色

label3.setForeground(Color.yellow);

        

        //新增按鈕

        jpanel=(JPanel)this.getContentPane();  

        jpanel.setLayout(null);   

        jbutton1.setBounds(new Rectangle(100,300,75,25));  

        jpanel.add(jbutton1);  

        jbutton1.addActionListener(new Action(this));

        jbutton2.setBounds(new Rectangle(250,300,75,25));  

        jpanel.add(jbutton2);  

        jbutton2.addActionListener(new Stop(this));

        

    }

    

/**

* 從控制檯輸入路徑

*/

public static String InputPath(){

String str ="";

System.out.println("請輸入TXT文字路徑:");

Scanner sc= new Scanner(System.in);

str = sc.nextLine();

return str;

}

/**

* 讀取文件資料

* @param filePath

* @return

*/

public static String ReadFile(String filePath){

String str = "";

try {

String encoding="GBK";

File file = new File(filePath);

if(file.isFile()&&file.exists()){

InputStreamReader reader = 

new InputStreamReader(new FileInputStream(file),encoding);

BufferedReader bufferedReader = new BufferedReader(reader);

String lineTxt = "";

while((lineTxt = bufferedReader.readLine()) != null){

str+=lineTxt+";\n";

}

                 reader.close();

}else{

            System.out.println("找不到指定的檔案");

        } 

}catch (Exception e) {

            System.out.println("讀取檔案內容出錯");

            e.printStackTrace();

        }

return str;

}

/**

* 將字串轉換為String陣列

*/

public static String[] ChangeType(String str){

ArrayList<String> list=new ArrayList<String>();

String[] string = str.split(";");

return string;

}

/**

* main方法

* @param args

*/

public static void main(String args[]){

點名器 jframe=new 點名器();

jframe.setTitle("點名器");

jframe.setSize(450,400);  

    jframe.setVisible(true);  

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jframe.setResizable(false);

    jframe.setLocationRelativeTo(null);

System.out.println();

}

/**

     * 點選確定按鈕後的方法

     */

    public void chooseValue(ActionEvent e){

    String str = "";

    str = jtext.getText();

    if(str != "" || str != null){

    點名器 jframe = new 點名器(str);

    jframe.setTitle("點名器");

jframe.setSize(450,400);  

    jframe.setVisible(true);  

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jframe.setResizable(false);

    jframe.setLocationRelativeTo(null);

System.out.println(str);

    }

    }

    /**

     * 點選開始按鈕後的方法

     */

    public void actionRun(ActionEvent e){

    if(flag){

//執行緒開始

    t = new Thread(new Runnable(){

    public void run(){//

                while(count<=10000){

                //檔案路徑

                String strTest = strPath;

                //開始讀取資料

                    String strRead = ReadFile(strTest);

                //將讀取到的資料變為陣列

                String[] strc = ChangeType(strRead);

                //獲取隨機的姓名

                Random random = new Random();

                int a = 0;

                a = random.nextInt(strc.length-1);

                String str = strc[a];

                System.out.println("輸出名字為:"+str);

                label3.setFont(new java.awt.Font(str,1,60));

                //設定名字標籤的文字

                label3.setText(str);

                    try{

                        t.sleep(20);//使執行緒休眠50毫秒

                    }catch(Exception e){

                    e.printStackTrace();

                    }

                    count+=1;//顯示次數

                }

            }

    });

    t.start();

    //設定字型顏色

jpanel.add(label3);

flag = false;

    }

    flag = false;

    }

    /**

     * 點選暫停按鈕後的方法

     */

    public void stopRun(ActionEvent e){

    if(!flag){

    t.stop();

    flag = true;

    }

    flag = true;

    }

}

 

/**

 *確定按鍵監控類

 */

class TextValue implements ActionListener {  

    private 點名器 startJFrame;  

    TextValu(點名器 startJFrame) {  

        this.startJFrame = startJFrame;  

   }

    public void actionPerformed(ActionEvent e) {  

    startJFrame.chooseValue(e); 

    startJFrame.setVisible(false);

    }

}

 

/**

 *開始按鍵監控類

 */

class Action implements ActionListener {  

    private 點名器 jFrameIng;  

    Action(點名器 jFrameIng) {  

        this.jFrameIng = jFrameIng;  

   }  

public void actionPerformed(ActionEvent e) {

jFrameIng.actionRun(e);

 

}

/**

 *暫停按鍵監控類

 */

class Stop implements ActionListener {  

    private 點名器 jFrameIng;  

    Stop(點名器 jFrameIng) {  

        this.jFrameIng = jFrameIng;  

   }  

public void actionPerformed(ActionEvent e) {

jFrameIng.stopRun(e);

    }  

}

 

 

本週學習總結:

1,事件監聽器

事件監聽器是類庫中的一組介面,每種事件類都有一個負責監聽這種事件物件的介面。介面中定義了處理該事件的抽象方法。
介面只是一個抽象定義,要想使用必須實現它。所以每次對事件進行處理是呼叫對應介面的實現類中的方法。當事件源產生事件並生成事件物件,該物件被送到事件處理器中,處理器呼叫介面實現類物件中的相應方法來處理該事件。
要想啟動相應的事件監聽器必須在程式中註冊它。

事件的種類

JAVA處理事件響應的類和監聽介面大多位於AWT包中。在java.swing.event包中有專門用於Swing元件的事件類和監聽介面。
AWT事件類繼承自AWTEvent,他們的超類是EventObject。在AWT事件中,事件分為低階事件和語義事件。語義事件是對某些低階事件的一種抽象概括,是單個或多個低階事件的某些特例的集合。
常用低階事件有:

事件 說明
KeyEvent 按鍵按下和釋放產生該事件
MouseEvent 滑鼠按下、釋放、拖動、移動產生該事件
FocusEvent 元件失去焦點產生該事件
WindowEvent 視窗發生變化產生該事件
常用語義事件有:

事件 說明
ActionEvent 當單擊按鈕、選中選單或在文字框中回車等產生該事件
ItemEvent 選中多選框、選中按鈕或者單擊列表產生該事件
常用事件和事件監聽器如下:

事件型別 對應的監聽器 監聽器介面中的抽象方法
Action ActionListener actionPerformed(ActionEvent e)
Mouse MouseListener mouseClicked(MouseEvent e)、mouseEntered(MouseEvent e)、mouseExited(MouseEvent e)、mousePressed(MouseEvent e)、mouseReleased(MouseEvent e)
MouseMotion MouseMotionListener mouseDragged(MouseEvent e)、mouseMoved(MouseEvent e)
Item ItemListener itemStateChanged(ItemEvent e)
Key KeyListener keyPressed(KeyEvent e)、keyReleased(KeyEvent e)、keyTyped(KeyEvent e)
Focus FocusListener focusGained(FocusEvent e)、focusLost(FocusEvent e)
Window WindowListener windowActivated(WindowEvent e)、windowClosed(WindowEvent e)、windowClosing(WindowEvent e)、windowDeactivated(WindowEvent e)、windowDeiconified(WindowEvent e)、windowIconified(WindowEvent e)、windowOpened(WindowEvent e)
Component ComponentListener componentHidden(ComponentEvent e)、componentMoved(ComponentEvent e)、componentResized(ComponentEvent e)、componentShown(ComponentEvent e)
Text TextListener textValueChanged(TextEvent e)
事件介面卡

事件介面卡其實就是一個介面的實現類,實際上介面卡類只是將監聽介面方法中的方法全部實現成空方法。這樣在定義事件監聽器時就可以繼承該實現類,並重寫所需要的方法,不必實現覆蓋所有方法了。常用的事件介面卡類有如下擊中

介面卡 說明
MouseAdapter 滑鼠事件介面卡
WindowAdapter 視窗事件介面卡
KeyAdapter 鍵盤事件介面卡
FocusAdapter 焦點介面卡
MouseMotionAdapter 滑鼠移動事件介面卡
ComponentAdapter 元件源介面卡
ContainerAdapter 容器源事件介面卡