Swing MouseAdapter類

2019-10-16 22:11:07

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

類宣告

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

public abstract class MouseAdapter
   extends Object
      implements MouseListener, MouseWheelListener, MouseMotionListener

類建構函式

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

類方法

編號 類方法 描述說明
1 void mouseClicked(MouseEvent e) 在元件上單擊(按下並釋放)滑鼠按鈕時呼叫。
2 void mouseDragged(MouseEvent e) 在元件上按下滑鼠按鈕然後拖動時呼叫。
3 void mouseEntered(MouseEvent e) 滑鼠進入元件時呼叫。
4 void mouseExited(MouseEvent e) 滑鼠退出元件時呼叫。
5 void mouseMoved(MouseEvent e) 將滑鼠游標移動到元件但未按下任何按鈕時呼叫。
6 void mousePressed(MouseEvent e) 在元件上按下滑鼠按鈕時呼叫。
7 void mouseReleased(MouseEvent e) 在元件上釋放滑鼠按鈕時呼叫。
8 void mouseWheelMoved(MouseWheelEvent e) 旋轉滑鼠滾輪時呼叫。

方法繼承

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

  • java.lang.Object

MouseAdapter範例

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

package com.yiibai.swing.listener;

import javax.swing.*;

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

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

    public MouseAdapterDemo() {
        prepareGUI();
    }

    public static void main(String[] args) {
        MouseAdapterDemo swingAdapterDemo = new MouseAdapterDemo();
        swingAdapterDemo.showMouseAdapterDemo();
    }

    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 showMouseAdapterDemo() {
        headerLabel.setText("Listener in action: MouseAdapter");
        JPanel panel = new JPanel();
        panel.setBackground(Color.blue);
        panel.setLayout(new FlowLayout());

        panel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                statusLabel.setText("Mouse Clicked: (" + e.getX() + ", " + e.getY() + ")");
            }
        });
        JLabel msglabel = new JLabel("歡迎您來到易百教學/Swing", JLabel.CENTER);

        msglabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                statusLabel.setText("Mouse Clicked: (" + e.getX() + ", " + e.getY() + ")");
            }
        });
        panel.add(msglabel);
        controlPanel.add(panel);
        mainFrame.setVisible(true);
    }
}

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

MouseAdapter