环球今热点:鼠标事件——MouseEventer接口的5个方法

发布时间:   来源:CSDN  

鼠标事件


(资料图)

鼠标事件 MouseEvent,包括以下动作:

mouseClicked   //鼠标点击mousePressed   //鼠标按下mouseReleased  //鼠标抬起mouseEntered   //鼠标移入mouseExited    //鼠标移除mouseMove      //鼠标移动mouseDragged   //鼠标拖拽mouseWheelMoved  //鼠标滚轮

所有动作分为三类监听器:

addMouseListener()   //点击、按下、抬起、移入、移出addMouseMotionListener()  //移动、拖拽addMouseWheelListener()  //鼠标滚轮转动

MouseListener接口有5个方法,每次实现时要重写它们:

panel.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseClicked(MouseEvent e) {}});

注意:鼠标点击事件(mouseClicked)触发的时机:按下和松开在同一个点。如果是判断双击和三机,有时间限制。如果是双击,会先出发单击;如果是三击,会先出发双击、单击

鼠标事件对象 MouseEvent:

getX() / getY()  //点击中的坐标,相对于该控件getXOnScreen() / getYOnScreen()  //相对于屏幕的坐标getSource()  //事件源,即点中的控件getButton()  //左键、中键、右键getClickedCount()  //单击、双击、三击

鼠标适配器

假设我们只需要鼠标点击事件,但是在实现 MouseListener 接口时发现要重写所有的 5 个方法,太冗余。

MouseAdapter 类实现了 MouseListener 接口和其它的鼠标监听器接口,方法里什么也没写,我们可以继承这个 MosueAdapter 类然后重写我们需要的方法。

panel.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {if(e.getButton() == MouseEvent.BUTTON1)System.out.println("左键");else if(e.getButton() == MouseEvent.BUTTON2) System.out.println("中键");else if(e.getButton() == MouseEvent.BUTTON3)System.out.println("右键");int cc = e.getClickCount();if(cc == 1)System.out.println("单击");else if(cc == 2)System.out.println("双击");else if(cc == 3)System.out.println("三击");System.out.println("鼠标位置: " + e.getX() + "," + e.getY());}});

练习1 图片查看器

需求:

加载文件夹里的图片显示显示缩略图点击缩略图时,显示大图当前图片高亮显示

Demo.java

package my;import java.awt.Container;import java.awt.Graphics;import java.awt.Image;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JLabel;public class Demo {public static void main(String[] args) {MyFrame frame = new MyFrame("Demo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(700, 300);frame.setVisible(true);}}

MyFrame.java

package my;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Image;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.util.ArrayList;import javax.imageio.ImageIO;import javax.swing.BorderFactory;import javax.swing.JFrame;import javax.swing.JPanel;public class MyFrame extends JFrame{MyPanel showPanel = new MyPanel();ArrayListthumbMyLabel = new ArrayList<>();public MyFrame(String title) {super(title);JPanel thumbPanel = new JPanel();thumbPanel.setLayout(new FlowLayout());File file = new File("./src/images");System.out.println(file.getAbsolutePath());File[] imagesPath = file.listFiles();for(File imagePath : imagesPath) {try {Image img = ImageIO.read(imagePath);MyLabel label = new MyLabel(img);label.addMouseListener(new MouseListener());thumbPanel.add(label);thumbMyLabel.add(label);}catch(Exception e) {e.printStackTrace();}}JPanel root = new JPanel();this.setContentPane(root);root.setLayout(new BorderLayout());root.add(thumbPanel, BorderLayout.NORTH);root.add(showPanel, BorderLayout.CENTER);}private class MouseListener extends MouseAdapter{@Overridepublic void mouseClicked(MouseEvent e) {MyLabel label = (MyLabel)e.getSource();for(MyLabel thumbLabel : thumbMyLabel) {if(thumbLabel == label) {thumbLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));}else {thumbLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 4));}}Image img = label.img;showPanel.setImage(img);showPanel.repaint();}}}

MyLabel.java

package my;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import javax.swing.JLabel;public class MyLabel extends JLabel{public Image img;private int imgW;private int imgH;public MyLabel(Image img) {this.img = img;imgW = img.getWidth(null);imgH = img.getHeight(null);this.setPreferredSize(new Dimension(50, 50 * imgH / imgW));this.setOpaque(false);}public void setImage(Image img) {this.img = img;}@Overrideprotected void paintComponent(Graphics g) {int width = this.getWidth();int height = this.getHeight();g.setColor(Color.LIGHT_GRAY);g.fillRect(0, 0, width, height);if(img != null) {g.drawImage(img, 0, 0, width, height, null);}}}

MyPanel.java

package my;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import javax.swing.JPanel;public class MyPanel extends JPanel{Image img;public void setImage(Image img) {this.img = img;}@Overrideprotected void paintComponent(Graphics g) {int width = this.getWidth();int height = this.getHeight();g.setColor(Color.WHITE);g.fillRect(0, 0, width, height);g.setColor(Color.BLACK);g.drawRect(0, 0, width, height);if(img != null) {int imgW = img.getWidth(null);int imgH = img.getHeight(null);int fitW = width;int fitH = fitW * imgH / imgW;if(fitH > height) {fitH = height;fitW = fitH * imgW / imgH;}int x = (width - fitW) / 2;int y = (height - fitH) / 2;g.drawImage(img, x, y, fitW, fitH, null);}}}

显示:

相关文章Related

返回栏目>>