代码如下:
package relevantTest;
/*
* 该类实现了对图片的任意大小的缩放处理,滚动鼠标滚轮对图片进行缩放处理,图片的高度和宽度最好不要超过屏幕的宽高
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
public class TestMouseWheelEvent extends JFrame{
private static final long serialVersionUID = 1L;
static Image img=(new ImageIcon("images/王梓盈.jpg")).getImage();
static int imgWidth=img.getWidth(null);//获得图片的宽度
static int imgHeight=img.getHeight(null);//获得图片的高度
static int ScreenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;//获得屏宽
static int ScreenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;//获得屏高
static int xCoor=(ScreenWidth-imgWidth)/2;
static int yCoor=(ScreenHeight-imgHeight)/2;
public TestMouseWheelEvent(){
PicsPanel jp=new PicsPanel();
jp.setBackground(new Color(51,51,51));
/*
* 添加鼠标滚轮事件
*/
jp.addMouseWheelListener(new MouseWheelListener(){
public void mouseWheelMoved(MouseWheelEvent e){
if(e.getWheelRotation()<0){//如果鼠标滚轮的“咔哒声”小于零则表示是向上滚动
imgWidth+=e.getScrollAmount()*10;
imgHeight+=e.getScrollAmount()*10;
xCoor=(ScreenWidth-imgWidth)/2;
yCoor=(ScreenHeight-imgHeight)/2;
repaint();
}
else{//否则表示向下滚动
imgWidth-=e.getScrollAmount()*3;
imgHeight-=e.getScrollAmount()*3;
xCoor=(ScreenWidth-imgWidth)/2;
yCoor=(ScreenHeight-imgHeight)/2;
repaint();//e.getScrollAmount()=3,如电脑的设置有关,一般的电脑默认是3
}
}
});
/**
* 在该面板上创建弹出右键菜单,“悬浮面板"
*/
JPopupMenu jpm=new JPopupMenu();
jpm.setBackground(new Color(51,51,51));
JMenuItem copy=new JMenuItem("复制(C)");
JMenuItem cut=new JMenuItem("剪切(X)");
JMenuItem open=new JMenuItem("打开图片所在的文件夹(O)");
JMenu setBackgroundColor=new JMenu("设置背景色(S)");
JMenuItem delete=new JMenuItem("删除(D)");
JMenuItem property=new JMenuItem("属性");
copy.setBackground(new Color(51,51,51));
copy.setForeground(new Color(204,204,204));
//copy.setFont(new Font("楷体",Font.BOLD,12));//为什么设置字体后导致弹出菜单出现变慢???
cut.setBackground(new Color(51,51,51));
cut.setForeground(new Color(204,204,204));
open.setBackground(new Color(51,51,51));
open.setForeground(new Color(204,204,204));
setBackgroundColor.setBackground(new Color(51,51,51));
setBackgroundColor.setForeground(new Color(204,204,204));
delete.setBackground(new Color(51,51,51));
delete.setForeground(new Color(204,204,204));
property.setBackground(new Color(51,51,51));
property.setForeground(new Color(204,204,204));
jpm.add(copy);
jpm.add(cut);
jpm.addSeparator();
jpm.add(open);
jpm.addSeparator();
jpm.add(setBackgroundColor);
jpm.addSeparator();
jpm.add(delete);
jpm.addSeparator();
jpm.add(property);
jp.setComponentPopupMenu(jpm);//确定弹出菜单的父组件
/**
* 添加菜单子项事件监听代码
*/
this.add(jp);
this.setTitle("测试鼠标滚轮事件");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//默认的框架显示位置是桌面左上角
this.setSize(1366,768);
this.setVisible(true);
}
static class PicsPanel extends JPanel{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,xCoor,yCoor,imgWidth,imgHeight,this);
}
}
public static void main(String[] args){
new TestMouseWheelEvent();
}
}