📄 mouseinputdemo.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class MouseInputDemo extends JFrame { private JPanel panel; private int startX, startY, endX, endY; public MouseInputDemo() {/* A JPanel object is placed on a Frame and registers a *//* MouseInputListener to receive both motion-oriented and *//* non motion-oriented MouseEvents. */ panel = new JPanel(); MouseInputHandler listener = new MouseInputHandler(); panel.addMouseListener(listener); panel.addMouseMotionListener(listener); getContentPane().add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 300, 200); setVisible(true); }/* The MouseInputListener is implemented as an inner class. When *//* the user presses the mouse inside the bounding area of the *//* panel, a MouseEvent is generated and sent to the mousePressed() *//* method. This method sets the starting point for the line. *//* When the mouse is dragged inside the panel, MouseEvents are *//* generated and sent to the mouseDragged() method which draws a *//* line according to the position of the mouse. Every method *//* declared in the MouseListener and MouseMotionListener interfaces *//* must be given an implementation. The other five methods are *//* implemented as stub methods. */ class MouseInputHandler implements MouseInputListener { public void mousePressed(MouseEvent event) { startX = event.getX(); startY = event.getY(); endX = startX; endY = startY; } public void mouseDragged(MouseEvent event) { startX = endX; startY = endY; endX = event.getX(); endY = event.getY(); panel.getGraphics().drawLine(startX, startY, endX, endY); } public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseMoved(MouseEvent event) {} } public static void main(String args[]) { MouseInputDemo demo = new MouseInputDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -