初学者非常非常虚心求教,怎样在SWT中的拖拽中实现半透明层并且跟综鼠标? 我尝试过添加DragDetectListener,但在拖拽过程中好像只能检测到鼠标按下的第一个坐标,之后没有一个Event可以一直追踪鼠标的move事件,DropTarget只能在接收区域内响应。我希望可以跟踪鼠标位置以实现自画图形的拖拽。还有在拖拽过程怎样能实现半透明层。如各位有知道相关的任何解释,请一定赐教。谢谢各位了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大家帮帮忙,在canvas上添加的监听器竟然没有实现鼠标跟踪的功能,我很怀疑,肯定是我哪里搞不对,我还在努力尝试中,如果各位路过看到可借只手指点迷津 使用JLayeredPane不行吗? 晚上回去给你看看 我用的eclipse,你说的那个是swing中的么?!谢谢谢谢你!终于有个人回话了!!!!!!!我等着你的教诲 对,Swing中的分层组件。别说的那么严重,教诲就谈不上了 public class Drag extends JFrame{ JButton b = null; public Drag(){ getContentPane().setLayout(null); b = new JButton("Test"); b.setBounds(2, 2, 50, 20); getRootPane().getLayeredPane().add(b, JLayeredPane.DEFAULT_LAYER); JPanel panel = new MyPanel(); panel.setBounds(0, 0, 500, 500); panel.setOpaque(false); getRootPane().getLayeredPane().add(panel, JLayeredPane.DRAG_LAYER); setSize(500, 500); } public static void main(String args[]) { JFrame frm = new Drag(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } private class MyPanel extends JPanel implements MouseListener, MouseMotionListener{ private boolean isPressOrDrag = false; private int x; private int y; private int width; private int height; public MyPanel() { super(); addMouseListener(this); addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { isPressOrDrag = b.contains(new Point(e.getX() - b.getX(), e.getY() - b.getY())); x = b.getX() + b.getWidth() / 2; y = b.getY() + b.getHeight() / 2; width = b.getWidth(); height = b.getHeight(); } public void mouseReleased(MouseEvent e) { if(isPressOrDrag){ isPressOrDrag = false; b.setLocation(x, y); repaint(); } } public void mouseDragged(MouseEvent e) { x = e.getX() - b.getWidth() / 2; y = e.getY() - b.getHeight() / 2; printComponent(getGraphics()); } public void printComponent(Graphics g) { if(isPressOrDrag){ Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.red); g2.drawRect(x, y, width, height); } super.printComponent(g); } }} 一个简单的实现,可能会有错误,自己改改吧。上面的方法不太好。还有中就是在拖拽的时候,获取指定区域的Image,设置Image的透明度,再与鼠标图片合并,松开后再换回鼠标的形状。也就是说可以通过自定义鼠标形状实现,自己看看吧 myeclipse无法进行图形界面编程 初级选手,求高手一起分析! 程序执行之后网络不通了,半个月了,问题一直没解决...... 问下关于继承的问题 JAVA怎么去除网页中含有某关键词的一行内容除掉? 请教:JTextField边输入边验证,如验证无效,清空JTextField 跪求高手奉献源代码,真的找不到答案,批处理启动TOMCAT,SQL。 static的具体意思? 关于SQL Server2000 数据库 不能创建的问题 java的那种开发工具比较好 import com.borland.dbswing.*;在ecplise不识别,没有这个包 按钮拖拽问题
我等着你的教诲
JButton b = null;
public Drag(){
getContentPane().setLayout(null);
b = new JButton("Test");
b.setBounds(2, 2, 50, 20);
getRootPane().getLayeredPane().add(b, JLayeredPane.DEFAULT_LAYER); JPanel panel = new MyPanel();
panel.setBounds(0, 0, 500, 500);
panel.setOpaque(false);
getRootPane().getLayeredPane().add(panel, JLayeredPane.DRAG_LAYER);
setSize(500, 500);
}
public static void main(String args[]) {
JFrame frm = new Drag();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
} private class MyPanel extends JPanel implements MouseListener, MouseMotionListener{
private boolean isPressOrDrag = false;
private int x;
private int y;
private int width;
private int height;
public MyPanel() {
super();
addMouseListener(this);
addMouseMotionListener(this);
} public void mousePressed(MouseEvent e) {
isPressOrDrag = b.contains(new Point(e.getX() - b.getX(), e.getY() - b.getY()));
x = b.getX() + b.getWidth() / 2;
y = b.getY() + b.getHeight() / 2;
width = b.getWidth();
height = b.getHeight();
} public void mouseReleased(MouseEvent e) {
if(isPressOrDrag){
isPressOrDrag = false;
b.setLocation(x, y);
repaint();
}
} public void mouseDragged(MouseEvent e) {
x = e.getX() - b.getWidth() / 2;
y = e.getY() - b.getHeight() / 2; printComponent(getGraphics());
} public void printComponent(Graphics g) {
if(isPressOrDrag){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.drawRect(x, y, width, height);
}
super.printComponent(g);
}
}
}
还有中就是在拖拽的时候,获取指定区域的Image,设置Image的透明度,再与鼠标图片合并,松开后再换回鼠标的形状。也就是说可以通过自定义鼠标形状实现,自己看看吧