Java实现简单的鼠标划线
import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.awt.Graphics; import java.lang.Math; import java.awt.*; public class test1 extends JPanel implements MouseListener, MouseMotionListener { static test1 nPanel; public static void main(String args[]){ JFrame nFrame=new JFrame("Drawing program"); nPanel=new test1(); nFrame.add(nPanel); nFrame.setBounds(100,100,800,800); nFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nFrame.setVisible(true); nPanel.addMouseListener(nPanel); } public void mousePressed(MouseEvent e) { int x=e.getX(); int y=e.getY(); Graphics g=getGraphics(); g.drawLine(0,0,x,y); } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } }