Swing常用事件

事件:ActionEvent

监听器:ActionListener

方法:void actionPerformed(ActionEvent e)

发生操作时调用。

·String getActionCommand()

actioncommand是控件内部的一个字符串,用于标识控件的状态。此函数可以取得控件的状态,从而决定到底该做什么。

·int getModifiers()

取得”产生此事件时,用户同时按下的组合键”

long getWhen()

取得“产生此事件时的时间戳”

事件产生原因:点击按钮,在列表框中选择内容,在文本框中点回车,计时器到期。
package MyFram;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MyAction extends JFrame implements ActionListener {
    JButton jb;
    public void init() {
        this.setTitle("窗口");
        this.setSize(200, 200);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public MyAction() {
        jb=new JButton("按钮");
        jb.addActionListener(this);
        this.getContentPane().setLayout(new FlowLayout());
        this.getContentPane().add(jb);
        init();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点击事件产生了");
    }
    public static void main(String[] args) {
        new MyAction();
    }
}

事件:AdjustmentEvent

监听器:AdjustmentListener

方法:void adjustmentValueChanged(AdjustmentEvent e)

在可调整的值发生更改时调用该方法。

·Adjustable getAdjustable()

adjustable是一个接口,各种滚动条都实现了这个借口。

·int getAdjustmentType()

取得滚动类型,共有UNIT_INCREMENT,UNIT_DECREMENT,BLOCK_INCREMENT,BLOCK_DECREMENT,TRACK共5种类型

·int getValue()

取得滚动滑块的当前值

事件产生原因:滚动条的滑动

/**
 * 滚动条的滚动
 */
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class AdjustmentListenerDemo extends JFrame implements AdjustmentListener {

    JTextArea area;
    JScrollBar  jb;

    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public AdjustmentListenerDemo() {
        init();
        area = new JTextArea();
        jb = new JScrollBar(JScrollBar.HORIZONTAL);
        jb.addAdjustmentListener(this);
        this.getContentPane().add(jb);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new AdjustmentListenerDemo();
    }

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("滑块产生了滑动");
    }

}

事件:ItemEvent

监听器:ItemListener

ItemListener

方法: void itemStateChanged(ItemEvent e)

在用户已选定或取消选定某项时调用。为此方法编写的代码将执行需要在已选定(或已取消选定)项时发生的操作。

·Object getItem()

取得被选中的元素。返回值时Object,应进行强转。

·ItemSelectable getItemSelectable()

ItemSelectable是一个接口,代表那些包含了n个可供选择的子元素的对象。此方法返回产生此事件的对象。此方法的作用主要在于,如果一个列表框是允许多选的,那么上面的方法就不好用了,应该用此方法得到列表对象,再取得被选中的多个元素。

·int getSateChange()

取得选择的状态,是SELECTED还是DESELECTED

事件产生原因:各种按钮的点击,列表框的选择
package com.lanqiao.demo3;
/**
 * 各种按钮的点击,列表框的选择
 */
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ItemListenerDemo extends JFrame implements ItemListener{
    //下拉列表
    JComboBox box;
    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public ItemListenerDemo() {
        this.init();
        box = new JComboBox();
        box.addItem("请选择");
        box.addItem("大学");
        box.addItem("高中");
        box.addItem("初中");

        //绑定事件
        box.addItemListener(this);
        //设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        //添加组件
        this.getContentPane().add(box);
        //显示窗体
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ItemListenerDemo();
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println(e.getItem());
        }
    }

}

事件:FocusEvent

监听器:FocusListener

方法:

·void focusGained(FocusEvent e)

组件获得键盘焦点时调用

·void focusLost(FocusEvent e)

组件失去键盘焦点时调用。

·Component getOppositeComponent()

得到:” 参与焦点变换的”另一个对象。如果是焦点取得事件,则返回之前拥有焦点的对象;如果是失去焦点事件,则返回得到焦点的对象。若焦点转移到另一个应用程序则返回null

·boolean isTemporary()

焦点是临时转移还是永久转移。临时性转移:用鼠标拖动窗口或者窗口变为非激活状态。其他为永久性转移。

事件产生原因:各组件的焦点变化。

/**
 * 各种组件的焦点变化
 */
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class FocusListenerDemo extends JFrame implements FocusListener{
    //下拉列表
    JTextField text1,text2;
    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public FocusListenerDemo() {
        this.init();
        text1 = new JTextField(20);
        text2 = new JTextField(20);

        //绑定事件
        text1.addFocusListener(this);
        //设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        //添加组件
        this.getContentPane().add(text1);
        this.getContentPane().add(text2);
        //显示窗体
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new FocusListenerDemo();
    }


    @Override
    public void focusGained(FocusEvent e) {
        System.out.println("获取焦点");
    }

    @Override
    public void focusLost(FocusEvent e) {
        System.out.println("失去焦点");
    }

}

事件:KeyEvent

监听器:KeyListener

方法:

·void keyPressed(KeyEvent e)

按下某个键时调用此方法

· void keyReleased(KeyEvent e)

释放某个键时调用此方法

·void keyTyped(KeyEvent e)

键入某个键时调用此方法

·char getKeyChar()

此方法只在keytype中有效,返回按下的字符。比如按下shift+a,返回‘A’

·int getKeyCode()

取得键盘上按键的整数编码。不支持组合键,Shift+a和a返回同样的内容,都是小写的a的ASCII码。但是大键盘的数字键和小键盘的数字键返回内容不同。

·static String getKeyModifiersText(int modifiers)

将掩码转换为字符串,比如Ctrl,Ctrl+Shift等

·static String getKeyText(int keyCode)

返回keyCode代表的按钮的名字,比如“Home”,“F1”等

事件产生原因:在各组件上敲击键盘
package com.lanqiao.demo3;
/**
 * 在各种组件上敲击键盘
 */
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class KeyListenerDemo extends JFrame implements KeyListener{
    //下拉列表
    JTextField text1;
    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public KeyListenerDemo() {
        this.init();
        text1 = new JTextField(20);

        //绑定事件
        text1.addKeyListener(this);
        //设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        //添加组件
        this.getContentPane().add(text1);
        //显示窗体
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new KeyListenerDemo();
    }


    @Override
    public void keyTyped(KeyEvent e) {
        //System.out.println("键入某个键时调用此方法");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("按下某个键时调用此方法");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //System.out.println("释放某个键时调用此方法");
    }


}

事件:MouseEvent

监听器:MouseListener

方法:

·void mouseClicked(MouseEvent e)

鼠标按键在组件上单击(按下并释放)时调用。

· void mouseEntered(MouseEvent e)

鼠标进入到组件上时调用。

·void mouseExited(MouseEvent e)

鼠标离开组件时调用。

·void mousePressed(MouseEvent e)

鼠标按键在组件上按下时调用。

·void mouseReleased(MouseEvent e)

鼠标按钮在组件上释放时调用。

·Point getLocationOnScreen()

返回鼠标相对于显示屏的绝对坐标

·int getXOnScreen()

返回鼠标相对于显示屏的X的绝对坐标

·int getYOnScreen()

返回鼠标相对于显示屏的Y的绝对坐标

·Point getPoint()

返回鼠标相对于组件的绝对坐标

·int getX()

返回鼠标相对于组件的X的绝对坐标

·int getY()

返回鼠标相对于组件的Y的绝对坐标

·void translatePoint(int x,int y)

给鼠标的作用点加上一个偏移量。比如2个参数分别是10,20,你要用鼠标画直线,当你鼠标指向100,100开始画,实际上的线是画在110,120处。

·int getClickCount()

产生此事件时鼠标共点击了多少下(单击、双击、三击、、、)

·int getButton()

产生此事件时,鼠标的哪个键被点击了:NOBUTTON, BUTTON1, BUTTON2 或者 BUTTON3

·boolean isPouupTrigger()

是否能够触发一个弹出式菜单。由于各个平台触发弹出式菜单的情况不一样,因此,应该在mousePressed和mouseReleased中都检测一下。

产生事件原因:在任何组件上点击鼠标、将光标移动进来或者出去
package com.lanqiao.demo3;
/**
 * 在任何组件上点击鼠标、将光标移动进来或出去
 */
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MouseListenerDemo extends JFrame implements MouseListener{

    //下拉列表
    JTextField text1;
    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public MouseListenerDemo() {
        this.init();
        text1 = new JTextField(20);
        //绑定事件
        text1.addMouseListener(this);
        //设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        //添加组件
        this.getContentPane().add(text1);
        //显示窗体
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MouseListenerDemo();
    }


    //单击鼠标时执行的操作  
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("单击鼠标时执行的操作  ");
    }
    // 鼠标在组件上按下时执行的操作  
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("鼠标在组件上按下时执行的操作  ");
    }
    // 鼠标释放时执行的操作  
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("鼠标释放时执行的操作  ");
    }
    // 鼠标进入组件时执行的操作 
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("鼠标进入组件时执行的操作 ");
    }
    // 鼠标离开组件时执行的操作  
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("鼠标离开组件时执行的操作  ");
    }

}

事件:MouseEvent

监听器:MouseMotionListener

·void mouseDragged(MouseEvent e)

鼠标按键在组件上按下并拖动时调用。在释放鼠标按键前,MOUSE_DRAGGED 事件被连续地传递到发起该拖动的组件(而不管鼠标位置是否处于该组件的边界内)。

·void mouseMoved(MouseEvent e)

鼠标光标移动到组件上但无按键按下时调用。

事件产生原因:在任何组件上移动鼠标
package com.lanqiao.demo3;
/**
 * 在任何组件上滚动鼠标中键
 */
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class MouseMotionListenerDemo extends JFrame implements MouseMotionListener{

    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        //this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public MouseMotionListenerDemo() {
        this.init();
        //绑定事件
        this.addMouseMotionListener(this);
        //设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        //添加组件
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MouseMotionListenerDemo();
    }


    //鼠标被拖动
    @Override
    public void mouseDragged(MouseEvent e) {
        System.out.println("鼠标被拖动");
    }
    //鼠标在移动
    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("鼠标在移动");
    }

}

事件:MouseWheelEvent

监听器:MouseWheelListener

方法:

· void mouseWheelMoved(MouseWheelEvent e)

鼠标滚轮旋转时调用。

·int getScrollType()

滚动的类型,是按块滚动WHEEL_BLOCK_SCROLL还是按单位滚动WHEEL_UNIT_SCROLL

·int getScrollAmount()

返回为相应此事件应该滚动的单位数。此方法有效的前提是按单位滚动。

·int getWheelRotation()

鼠标滚轮滚动过程中“咔嚓”声的数目。向上滚动为负值,向下滚动为正值。

产生事件的原因:在任何组件上滚动鼠标中健

事件:WindowEvent

监听器:WindowListener

方法:

·void windowActivated(WindowEvent e)

将 Window 设置为活动 Window 时调用。

·void windowClosed(WindowEvent e)

因对窗口调用 dispose 而将其关闭时调用。

· void windowClosing(WindowEvent e)

用户试图从窗口的系统菜单中关闭窗口时调用。

· void windowDeactivated(WindowEvent e)

当 Window 不再是活动 Window 时调用。

·void windowDeiconified(WindowEvent e)

窗口从最小化状态变为正常状态时调用。

· void windowIconified(WindowEvent e)

窗口从正常状态变为最小化状态时调用。

·void windowOpened(WindowEvent e)

窗口首次变为可见时调用。

·Window getWindow()

返回发生此事件的window对象

·Window getOppositeWindow()

若发生了焦点转移,则返回另一个参与此事件的window对象,或者null

·int getOldState()

窗口变化前的状态:NORMAL、ICONIFIED、MAXIMIZED_BOTH

·int getNewState()

窗口变化后的状态

产生事件的原因:窗口发生变化
package com.lanqiao.demo3;
/**
 * 窗口发生变化
 */
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

public class WindowListenerDemo extends JFrame implements WindowListener {

    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        // this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public WindowListenerDemo() {
        this.init();
        // 绑定事件
        this.addWindowListener(this);
        // 设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        // 添加组件
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new WindowListenerDemo();
    }

    // 窗口被完全激活时调用的方法
    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println("窗口被完全激活时调用的方法");
    }

    // 窗口的关闭按钮被点击时调用的方法
    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println("窗口的关闭按钮被点击时调用的方法");
    }

    // 窗口被完全关闭时调用的方法
    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("窗口被完全关闭时调用的方法");
    }

    // 窗口被最小化时调用的方法
    @Override
    public void windowIconified(WindowEvent e) {
        System.out.println("窗口被最小化时调用的方法");
    }

    // 窗口被从最小化还原时调用的方法
    @Override
    public void windowDeiconified(WindowEvent e) {
        System.out.println("窗口被从最小化还原时调用的方法");
    }

    // 窗口被完全激活时调用的方法
    @Override
    public void windowActivated(WindowEvent e) {
        System.out.println("窗口被完全激活时调用的方法");
    }

    // 窗口失去活性时调用的方法
    @Override
    public void windowDeactivated(WindowEvent e) {
        System.out.println("窗口失去活性时调用的方法");
    }

}

事件WindowEvent

监听器:WindowFocusListener

方法:

·void windowGainedFocus(WindowEvent e)

该 Window 被设置为聚焦 Window 时调用,聚焦 Window 意味着该 Window 或其某个子组件将接收键盘事件。

·void windowLostFocus(WindowEvent e)

该 Window 不再是聚焦 Window 时调用,不再是聚焦 Window 意味着不再传递键盘事件到该 Window 或其任意子组件。

产生事件原因:窗口得到或失去焦点
package com.lanqiao.demo3;
/**
 * 窗口得到或失去焦点
 */
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JFrame;

public class WindowFocusListenerDemo extends JFrame implements WindowFocusListener{

    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        // this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public WindowFocusListenerDemo() {
        this.init();
        // 绑定事件
        this.addWindowFocusListener(this);
        // 设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        // 添加组件
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new WindowFocusListenerDemo();
    }

    //该 Window 被设置为聚焦 Window 时调用
    @Override
    public void windowGainedFocus(WindowEvent e) {
        System.out.println("该 Window 被设置为聚焦 Window 时调用");
    }
    //该 Window 不再是聚焦 Window 时调用
    @Override
    public void windowLostFocus(WindowEvent e) {
        System.out.println("该 Window 不再是聚焦 Window 时调用");
    }

}

事件:WindowEvent

监听器:WindowStateListener

方法:

·void windowStateChanged(WindowEvent e)

窗口状态改变时调用。

产生事件原因:窗口状态发生变化
package com.lanqiao.demo3;
/**
 * 窗口状态改变
 */
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;

public class WindowStateListenerDemo extends JFrame implements WindowStateListener{

    public void init() {
        // 定义窗体的特征
        // 窗体标题
        this.setTitle("我的第一个JFrame程序");
        // 窗体大小
        this.setSize(300, 300);
        // 禁用放大按钮
        // this.setResizable(false);
        // 窗体位置(居中显示)
        this.setLocationRelativeTo(null);
        // 窗体关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public WindowStateListenerDemo() {
        this.init();
        // 绑定事件
        this.addWindowStateListener(this);
        // 设置布局
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
        // 添加组件
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new WindowStateListenerDemo();
    }

    @Override
    public void windowStateChanged(WindowEvent e) {
        System.out.println("窗口状态改变");
    }

}
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值