1.鼠标右键点击的事件前加个if判断:
if(canClick){
鼠标点击事件事件
}
2.然后添加键盘事件implements KeyListener
在重写的方法的keyPressed中加入:
if(e.getKeyCode()==KeyEvent.VK_CONTROL){
canClick=false
}
在重新的方法的keyReleased中加入:
if(e.getKeyCode()==KeyEvent.VK_CONTROL){
canClick=true
}
画图不能这样来画的,因为Graphics 不能自己来控制:DrawLine DW=new DrawLine()
DW.g.setColor(Color.orange)
DW.g.drawLine(orgX, orgY, e.getX(), e.getY())
public void mouseRealeased(MouseEvent e)这个方法你拼错了,应该是:
public void mouseReleased(MouseEvent e)
帮你改了下,实现的功能:安下鼠标不放 -->移动别处 -->松开,画图
import java.awt.Frame
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JPanel
public class DrawLine extends Frame {
static JPanel drawPanel
static int orgX
static int orgY
static int eX
static int eY
public static void main(String[] args) {
// TODO 自动生成方法存根
drawPanel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g)
Graphics2D g2D = (Graphics2D) g
g2D.drawLine(orgX, orgY, eX, eY)
}
}
DrawLine mainFrame = new DrawLine()
mainFrame.setSize(400, 400)
mainFrame.setTitle("画线")
mainFrame.add(drawPanel)
mainFrame.setVisible(true)
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose()
System.exit(0)
}
})
mainFrame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgX = e.getX()
orgY = e.getY()
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased")
eX = e.getX()
eY = e.getY()
drawPanel.repaint()
}
})
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)