e.getModifiers() & InputEvent.BUTTON1_MASK) = e.getModifiers(
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/30 09:06:52
e.getModifiers() & InputEvent.BUTTON1_MASK) = e.getModifiers(
e.getModifiers() & InputEvent.BUTTON1_MASK) = e.getModifiers(
e.getModifiers() & InputEvent.BUTTON1_MASK) = e.getModifiers(
求求你下回抄程序也吵得认真点吧,错误太多了,这个是改完的,自己看吧
package com.opt.test;
import java.awt.*;
import java.awt.event.*;
class MyCanvas extends Canvas implements MouseListener
{
int left=-1,right=-1;
int x=-1,y=-1;
MyCanvas()
{
setSize(200,200);
setBackground(Color.pink);
super.addMouseListener(this);
}
public void paint(Graphics g)
{
if(left==-1)
{
g.drawString("按下了鼠标左键",x,y);
}
else if(right==-1)
{
g.drawString("按下了鼠标右键",x,y);
}
}
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{
left=1;
right=-1;
repaint();
}
else if(e.getModifiers()==InputEvent.BUTTON2_MASK)
{
right=1;
left=-1;
repaint();
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e)
{
right=-1;
left=-1;
repaint();
}
public void mouseClicked(MouseEvent e){}
public void update (Graphics g)
{
if (left==1||right==1)
{
paint(g);
}
else
{
super.update(g);
}
}
}
public class a
{
public static void main (String args[])
{
Frame f=new Frame ();
f.setBounds (100,100,300,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void WindowCloseing(WindowEvent e)
{
System.exit(0);
}
});
f.setLayout(new FlowLayout());
f.add(new MyCanvas());//添加画布
f.validate();
}
}