swing中一些常见的鼠标事件

本文详细介绍了Swing库中关于鼠标形状变化、提示显示、图标切换、字体颜色响应、鼠标拖拽、关闭按钮行为、复选框与单选框选择以及系统托盘和对话框的使用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swing中一些常见的鼠标事件(鼠标形状的改变、鼠标经过时提示内容、鼠标接近、按下(图标的切换)、鼠标接近、离开(字体颜色的改变)、鼠标拖拽等)+ 一些常见的花样(关闭按钮(关闭前弹出对话框)、复选框的选中等)还有系统托盘+对话框

1.鼠标形状的改变:鼠标接触到控件时,鼠标发生了形状改变,例如变成了手形:

代码:控件对象 .setCursor(Cursor对象(手形的常量参数));

(例子里的lblRegNew是一个控件对象)

2.鼠标经过控件时出现提示内容:鼠标接触到控件时,浮现出提示内容:

代码:控件对象 .setToolTipText("提示的信息");

(效果:

3.鼠标接近、按下(图标的切换):对于图标控件,鼠标接近或按下的图片发生切换:

鼠标接近控件时图片发生切换代码:

imageButton.setRolloverIcon(new ImageIcon(nearButtonPath));     (例子里的imageButton是带图标的按钮控件,nearButtonPath 是一张图片的路径)

鼠标按下控件图片发生切换代码:

imageButton.setPressedIcon(new ImageIcon(buttonPath));            (例子里的imageButton是带图标的按钮控件,buttonPath 是一张图片的路径)

4.鼠标接近、离开(字体颜色的改变):鼠标接近控件时,字体发生改变,离开控件时字体颜色改变为原来的颜色:(由于java内部里没有鼠标接近、离开变字体色方法,让我们可以直接拿来用,

这里需要添加事件:监听鼠标的接近与离开)

代码:

//添加鼠标接近、离开标签字体颜色改变事件
    private void fontColorEvent() {
        lblRegister.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                lblRegister.setForeground(Color.BLUE);    
            }

            @Override
            public void mouseExited(MouseEvent e) {
                lblRegister.setForeground(Color.GRAY);    
            }
            
        });
    }

5.鼠标拖拽:鼠标拖拽窗体的事件(java的默认拖拽是需要你鼠标点击位置在窗体标题栏的那个位置,在内容面板你拖拽是没有反应的,所以我们需要添加事件:监听鼠标拖拽)

代码:

package com.xuetang9.kenny;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseMotionListener;

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

public class MouseMotionListenerDemo extends JFrame{
    public MouseMotionListenerDemo(){
        //设置可见性
                this.setVisible(true);
                //设置大小
                this.setSize(500, 400);
                //设置居中
                this.setLocationRelativeTo(null);
                //设置关闭退出
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //获取面板内容
                JPanel contentPane = (JPanel) getContentPane();
                //添加鼠标事件
                MouseEvent();
            
    }
    /**
     * 鼠标事件(包括了鼠标的移动、拖拽事件)
     */
    private void MouseEvent() {
        //给窗体对象添加鼠标的移动事件(添加MouseMotionListener的接口)
        this.addMouseMotionListener(new MouseListener() { });
    }
    //内部类封装鼠标事件
    public class MouseListener implements  MouseMotionListener{

        Point point;
        //鼠标拖拽事件
        @Override
        public void mouseDragged(java.awt.event.MouseEvent e) {
            //第一次拖拽,如果位置为空,设置鼠标位置为当前位置
            if(point == null) {
//                point.x = e.getPoint().x;
//                point.y = e.getPoint().y;
                point = e.getPoint();
            }
            //相对位置--鼠标相对于面板的移动
            Point relativePoint = new Point(e.getX() - point.x, e.getY() - point.y);
            //窗体相对屏幕移动,设置窗体的位置:窗体在屏幕的位置+移动的距离,e.getComponent().getLocationOnScreen()相对于屏幕的距离
            Point framePoint = new Point(relativePoint.x + e.getComponent().getLocationOnScreen().x, relativePoint.y + e.getComponent().getLocationOnScreen().y);
            //设置对象窗体位置
            MouseMotionListenerDemo.this.setLocation(framePoint);
        }
        //鼠标移动事件
        @Override
        public void mouseMoved(java.awt.event.MouseEvent e) {
            //实现获取相对于面板鼠标位置
            setTitle("鼠标移动:" + e.getX() + "," + e.getY());
            
        }
        
    }
    public static void main(String[] args) {
        new MouseMotionListenerDemo().setVisible(true);;
    }
    
}

 

6、★关闭按钮的话,需要添加销毁当前窗口的事件:(再加点花样,关闭前弄一个提示框问用户是否关闭):为按钮添加事件,提示框提醒用户是否关闭窗体:

代码:

//为按钮添加事件,提示框提醒用户是否关闭窗体
    private void btnEvent() {
        imageButton.addActionListener(new ActionListener() {    //imageButton是一个带图标的关闭按钮控件
            @Override
            public void actionPerformed(ActionEvent e) {
                //以对话框形式提醒用户,这里调用了工具类JOptionPane的showConfirmDialog方法
                int result = JOptionPane.showConfirmDialog(QQLayoutLoginDesign.this, "是否真的要残忍抛下小可爱");
                //看返回值是否与工具类JOptionPane的属性OK_OPTION一致,是的话销毁控件对象
                if(result == JOptionPane.OK_OPTION) {
                    QQLayoutLoginDesign.this.dispose();      //销毁本窗体对象(QQlayOUtLoginDesign是我自己定义的一个窗体对象)
                }
            }
        });   
}

7、复选框的选中(还有单选框的选择:加个分组实现选中单选框引发事件):

 代码:

package com.xuetang9.kenny;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

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

/**
 *组合框(下拉)
 * @author Huangyujun
 *
 */
public class MyJCombobox extends JFrame{
    private JPanel contentPane = null;
    //组合框
    private JComboBox<String> combobox = new JComboBox<String>(new String[] {
            "北京", "上海", "广州", "深圳"
    });
    
    public MyJCombobox() {
        //设置一下标题
        setTitle("组合框框");
        //设置一下大小
        setSize(500, 400);
        //设置居中
        setLocationRelativeTo(null);
        //设置退出模式
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //内容面板
        contentPane = (JPanel) getContentPane();
        //其他部分的初始化工作
        initComponnents();
    }

    private void initComponnents() {
        //设置内容面板为无布局
        contentPane.setLayout(null);
        //设置组合框边界
        combobox.setBounds(0, 0, 90, 30);
        //添加组合框
        contentPane.add(combobox);
        //添加点击事件
        combobox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("当前选中的是:" + combobox.getSelectedItem());
                
            }        
        });
        //设置深圳为默认选择
        combobox.setSelectedIndex(3);
//        combobox.addItemListener(new ItemListener() {
//
//            @Override
//            public void itemStateChanged(ItemEvent e) {
//                System.out.println("当前选中的是:" + combobox.getSelectedItem());
//            }
//            
//        });
    }
    public static void main(String[] args) {
        new MyJCombobox().setVisible(true);
    }
}

还有单选框的选择:加个分组实现选中单选框引发事件

8、系统托盘

9、对话框:

消息框showMessageDialog、输入框showInputDialog、组合框showOptionDialog、确认框showConfirmDialog。

messageType有:ERROR_MESSAGE 、INFORMATION_MESSAGEWARNING_MESSAGE 、QUESTION_MESSAGE 、PLAIN_MESSAGE 。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder-coco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值