| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 737 人关注过本帖
标题:求助 一道java题
取消只看楼主 加入收藏
ft5032839
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2004-11-30
收藏
 问题点数:0 回复次数:1 
求助 一道java题

要求: 在一面板内:单击鼠标左键放置一小方块,双击檫除,可以拖拽方块,当鼠标在方块中时,光标形状为精确定位. 以下是小弟写的,不知道,为什么就是看不到方块

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.awt.geom.*; public class Mousetest { public static void main(String[] args) { MouseFrame frame= new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class MouseFrame extends JFrame { public MouseFrame() { setTitle("MouseTest"); setSize(300,200); MousePanel panel=new MousePanel(); Container contentPane=getContentPane(); contentPane.add(panel); } } class MousePanel extends JPanel { private static final int SI=10; private ArrayList squares; private Rectangle2D current; public MousePanel() { squares=new ArrayList(); current=null; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); } public void paintConponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g; for(int i=0;i<squares.size();i++) g2.draw((Rectangle2D)squares.get(i)); } public Rectangle2D find(Point2D p) { for(int i=0;i<squares.size();i++) { Rectangle2D r=(Rectangle2D)squares.get(i); if(r.contains(p)) return r; } return null; } public void add(Point2D p) { double x=p.getX(); double y=p.getY(); current=new Rectangle2D.Double(x-SI/2,y-SI/2,SI,SI); squares.add(current); repaint(); } public void remove(Rectangle2D s) { if(s==null) return; if(s==current) current=null; squares.remove(s); repaint(); } private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent event) { current=find(event.getPoint()); if(current==null) add(event.getPoint()); } public void mouseClicked(MouseEvent event) { current=find(event.getPoint()); if(current!=null&&event.getClickCount()>=2) remove(current); } } private class MouseMotionHandler implements MouseMotionListener { public void mouseMoved(MouseEvent event) { if(find(event.getPoint())==null) setCursor(Cursor.getDefaultCursor()); else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); } public void mouseDragged(MouseEvent event) { if(current!=null) { int x=event.getX(); int y=event.getY(); current.setFrame(x-SI/2,y-SI/2,SI,SI); repaint(); } } } }

搜索更多相关主题的帖子: java 
2004-12-19 16:37
ft5032839
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2004-11-30
收藏
得分:0 

自己先顶顶.

斑竹,帮忙看看

2004-12-19 16:40
快速回复:求助 一道java题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034127 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved