java实现鼠标拖拽画矩形框
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassRectextendsJFr...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rect extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rect r=new Rect();
}
MyPanel mp=null;
public Rect()
{
mp=new MyPanel();
this.add(mp);
this.addMouseListener(mp);
//this.addMouseMotionListener(mp);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements MouseListener
{
int x1=0;
int y1=0;
int x2=0;
int y2=0;
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(x1, y1, x2, y2);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
x2=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX()-x1;
y2=e.getY()-y2;
this.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
小弟谢了一个程序 编译过去了 但是报异常运行不了 请高手帮忙看看是哪出错了? 展开
import javax.swing.*;
import java.awt.event.*;
public class Rect extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rect r=new Rect();
}
MyPanel mp=null;
public Rect()
{
mp=new MyPanel();
this.add(mp);
this.addMouseListener(mp);
//this.addMouseMotionListener(mp);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements MouseListener
{
int x1=0;
int y1=0;
int x2=0;
int y2=0;
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(x1, y1, x2, y2);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
x2=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX()-x1;
y2=e.getY()-y2;
this.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
小弟谢了一个程序 编译过去了 但是报异常运行不了 请高手帮忙看看是哪出错了? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询