/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DragTest;
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics.*;
import java.util.*;
import java.awt.event.*;
//definition of the TableManager class
class TableManager
{
public TableManager(final JLayeredPane diningHall)
{
int hallWidth = diningHall.getWidth(); //get the width of the diningHall
int hallHeight = diningHall.getHeight(); //get the height of the diningHall
Table tempTable = new Table(); //a temporary table for getting the table size
int tableAreaX = Table.getTableSize().width + 20; //horizotal size of the table area
int tableAreaY = Table.getTableSize().height + 20; //horizotal size of the table area
int horiQuantity = hallWidth / tableAreaX; //table quantity in the horizontal direction
int vertQuantity = hallHeight / tableAreaY; //table quantity in the vertical direction
int tableQuantity = horiQuantity * vertQuantity; //get the quantity of the tables that the hall may contain
tableGroup = new ArrayList<Table>(tableQuantity); //initialize the table collection
draggingImage = new DraggingImage("/ImageResources/bookingClock.png"); //initialize the dragging image as a booking clock
//set the mouse listener
mouseListener = new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent evt)
{
Table evtSource = ((Table)evt.getSource()); //get the event source
mouseEnteredTableArea = evtSource; //mark the mouseEnteredTableArea
evtSource.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//set the proper table image
if (evtSource.getTableState() == TableState.AVAILABLE)
{
evtSource.setTableImage("/ImageResources/emptyTableWhenSelected.png");
}
else if (evtSource.getTableState() == TableState.BOOKED)
{
evtSource.setTableImage("/ImageResources/bookedTableWhenSelected.png");
}
else if (evtSource.getTableState() == TableState.USING)
{
evtSource.setTableImage("/ImageResources/usingTableWhenSelected.png");
}
}
@Override
public void mouseExited(MouseEvent evt)
{
Table evtSource = ((Table)evt.getSource()); //get the event source
mouseEnteredTableArea = null; //mark the mouseEnteredTableArea
if (!draggingTag)
{
evtSource.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
//set the proper table image
if (evtSource.getTableState() == TableState.AVAILABLE)
{
evtSource.setTableImage("/ImageResources/emptyTable.png");
}
else if (evtSource.getTableState() == TableState.BOOKED)
{
evtSource.setTableImage("/ImageResources/bookedTable.png");
}
else if (evtSource.getTableState() == TableState.USING)
{
evtSource.setTableImage("/ImageResources/usingTable.png");
}
}
@Override
public void mouseReleased(MouseEvent evt)
{
Table evtSource = ((Table)evt.getSource()); //get the event source
diningHall.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
//set the state of the source table
if (mouseEnteredTableArea != null &&
mouseEnteredTableArea.getTableState() == TableState.AVAILABLE &&
evtSource.getTableState() == TableState.BOOKED)
{
evtSource.setTableAsAvailable(); //set the source table as available
mouseEnteredTableArea.setTableAsBooked(); //set the target table as booked
mouseEnteredTableArea.setTableImage("/ImageResources/bookedTableWhenSelected.png"); //set the table image as selected
draggingTag = false; //reset the dragging tag
draggingImage.setVisible(false); //set the visibility of the dragging image
diningHall.remove(draggingImage); //remove the dragging image from the dinning hall
}
else if (evtSource.getTableState() == TableState.BOOKED)
{
evtSource.setTableAsBooked(); //reset the booked table state when table exchanging fails
draggingTag = false; //reset the dragging tag
draggingImage.setVisible(false); //set the visibility of the dragging image
diningHall.remove(draggingImage); //remove the dragging image from the dinning hall
}
}
};
//set the mouse motion listener
mouseMotionListener = new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent evt)
{
Table evtSource = ((Table)evt.getSource()); //get the event source
if (evtSource.getTableState() == TableState.BOOKED)
{
evtSource.setTableImage("/ImageResources/bookedTableWhenExchanging.png");
diningHall.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
if (!draggingTag)
{
draggingTag = true; //set the dragging tag
draggingImage.setSize(draggingImage.getImageSize());
diningHall.add(draggingImage); //add the dragging image into the diningHall
diningHall.moveToFront(draggingImage); //set the dragging image above any table
draggingImage.setVisible(true); //set the visibility of the dragging image
}
draggingImage.setLocation(evtSource.getX() + evt.getX() - Table.getTableSize().width / 2,
evtSource.getY() + evt.getY() - Table.getTableSize().height / 2); //set location of the dragging image
}
}
};
//initialize the diningHall
for (int i = 0; i < vertQuantity; ++i)
{
for (int j = 0; j < horiQuantity; ++j)
{
tableInstance = new Table();
tableInstance.setLocation(j * tableAreaX + 10, i * tableAreaY + 10); //set the location of the table panel
//set mouse listener
tableInstance.addMouseListener(mouseListener);
//set mouse motion listener
tableInstance.addMouseMotionListener(mouseMotionListener);
tableGroup.add(tableInstance); //add a new table into the table collection
diningHall.add(tableInstance); //add the new table into the diningHall
}
}
}
private ArrayList<Table> tableGroup; //collection of the tables
private Table tableInstance = null; //a instance of the Table class
private MouseAdapter mouseListener = null; //a mouse listener
private MouseMotionAdapter mouseMotionListener = null; //a mouse motion listener
private Table mouseEnteredTableArea = null; //the table area that the mouse entered
private DraggingImage draggingImage = null; //the image to display when dragging
private boolean draggingTag = false; //tag to specify whether it is in dragging state
}
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA 鼠标拖放效果示例
共13个文件
png:8个
java:4个
jar:1个
4星 · 超过85%的资源 需积分: 9 109 下载量 120 浏览量
2009-06-08
23:49:53
上传
评论
收藏 141KB RAR 举报
温馨提示
Netbeans平台下实现的JAVA 拖放效果的一个实例,包括源代码和所需的图片资源。 (运行后会全屏显示窗体,没有关闭按钮,可使用快捷键Ctrl + F4关闭~)
资源推荐
资源详情
资源评论
收起资源包目录
JAVA DragAndDrop.rar (13个子文件)
DragTest.jar 92KB
Code And Resources
ImageResources
emptyTable.png 8KB
usingTable.png 9KB
emptyTableWhenSelected.png 9KB
usingTableWhenSelected.png 11KB
bookedTable.png 9KB
bookingClock.png 10KB
bookedTableWhenSelected.png 11KB
bookedTableWhenExchanging.png 9KB
DragTest
Table.java 3KB
TableManager.java 8KB
DraggingImage.java 1KB
DragTestFrame.java 3KB
共 13 条
- 1
资源评论
- 天涯泪小武2013-07-18没什么用处,不是想要的效果
- bthumous2013-10-21还可以,但是不是想要的
HHTecShare
- 粉丝: 4
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功