主题:[讨论]问题
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame implements MouseListener {
/**
* @param args the command line arguments
*/
static Frame f=new Frame();
static int x1=0,x2=0,y1=0,y2=0;
public static void main(String[] args) {
// TODO code application logic here
f.setTitle("鼠标事件");
f.setSize(150, 200);
f.addMouseListener(f);
f.setVisible(true);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=getGraphics();
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.blue);
int temp;
if(x1>x2)
{ temp=x1;
x1=x2;
x2=temp;
}
if(y1>y2)
{
temp=x1;
x1=x2;
x2=temp;
}
g.drawRect(x1, y1, x2-x1, y2-y1);
}
}
有什么问题呢?
import java.awt.event.*;
public class MyFrame extends Frame implements MouseListener {
/**
* @param args the command line arguments
*/
static Frame f=new Frame();
static int x1=0,x2=0,y1=0,y2=0;
public static void main(String[] args) {
// TODO code application logic here
f.setTitle("鼠标事件");
f.setSize(150, 200);
f.addMouseListener(f);
f.setVisible(true);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=getGraphics();
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.blue);
int temp;
if(x1>x2)
{ temp=x1;
x1=x2;
x2=temp;
}
if(y1>y2)
{
temp=x1;
x1=x2;
x2=temp;
}
g.drawRect(x1, y1, x2-x1, y2-y1);
}
}
有什么问题呢?