Java JInternalFrames收听JDesktopPane事件
有什么简单的方法可以让我的jinternalframes两全其美吗Java JInternalFrames收听JDesktopPane事件,java,mouselistener,jinternalframe,jlayeredpane,jdesktoppane,Java,Mouselistener,Jinternalframe,Jlayeredpane,Jdesktoppane,有什么简单的方法可以让我的jinternalframes两全其美吗 当我希望它们作为jdesktoppane的一部分时嵌入 也可以移动,能够处理自己的事件 希望这张照片能有所帮助。现在,我有我的代码将物品拖到你的播放器上,并将其插入下一个可用的背包位置,但我也希望能够将其拖到任何背包位置,但正如你所看到的,它在下面绘制。有没有什么快速而肮脏的方法来补救这个问题?我的JDesktopPane在顶部有一个面板,在那里所有的东西都被涂上了漆(当然除了jinternalframes)。对不起,所有
- 当我希望它们作为jdesktoppane的一部分时嵌入
- 也可以移动,能够处理自己的事件
theDesktop.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
updateMouseCursor(e);
if (MapGamePanel.draggingItem){
Point point = e.getPoint();
MapGamePanel.currentX = point.x;
MapGamePanel.currentY = point.y;
}
}
@Override
public void mouseMoved(MouseEvent e) {
updateMouseCursor(e);
}
});
theDesktop.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1){ //left
int whiteLeftSpace = (theDesktop.getWidth() - xTiles*32)/2;
boolean inGamePanel = e.getX() > whiteLeftSpace && e.getX() < (xTiles*32 + whiteLeftSpace) && e.getY() < yTiles*32;
String globalCoords = localToGlobalCoords((e.getX()-whiteLeftSpace)/32 + "," + e.getY()/32);
if (inGamePanel){
//looking for tiles with no mobs or players and loot
String[] globalCoordsSplit = globalCoords.split(",");
int globalX = Integer.parseInt(globalCoordsSplit[0]);
int globalY = Integer.parseInt(globalCoordsSplit[1]);
if ((!(globalX == characterX && globalY == characterY)) && //not under character
((globalX-1) == characterX || globalX == characterX || (globalX+1) == characterX) && //(+/-) 1 xTile
((globalY-1) == characterY || globalY == characterY || (globalY+1) == characterY)){ //(+/-) 1 yTile
HashMap <String, String> dropsToDrag = new HashMap <String, String>();
Boolean mobPresent = false;
Boolean playerPresent = false;
if (MapGamePanel.entityInfoHashTable.containsKey(globalCoords)){ //no mobs/npcs
mobPresent = true;
}
Iterator<Entry<String, String>> it = MapGamePanel.entityInfoHashTable.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> pairs = it.next();
String key = pairs.getKey();
if (!key.contains(",") && !key.contains("-")){
String[] values = pairs.getValue().split("\\|");
String tempCoords = values[0];
if (globalCoords.equals(tempCoords)){ //player on spot
playerPresent = true;
}
} else if (key.contains("-")){
String[] splitKey = key.split("-");
if (splitKey[0].equals(globalCoords)){
dropsToDrag.put(key, pairs.getValue());
}
}
}
int smallEntityId = Integer.MAX_VALUE; //2147483647
if (!mobPresent && !playerPresent && !dropsToDrag.isEmpty()){
Iterator<Entry<String, String>> it2 = dropsToDrag.entrySet().iterator();
while (it2.hasNext()) {
Entry<String, String> pairs = it2.next();
String[] keyWithPK = pairs.getKey().split("-");
String tmpCoords = keyWithPK[0];
String[] coordsSplit = tmpCoords.split(",");
int tempX = Integer.parseInt(coordsSplit[0]);
int tempY = Integer.parseInt(coordsSplit[1]);
int tmpEntityId = Integer.parseInt(keyWithPK[1]);
String[] values = pairs.getValue().split("\\|");
String tmpId = values[0];
int tmploot_amt = Integer.parseInt(values[1]);
String tmploot_filename = values[2];
if (tmpEntityId < smallEntityId){
smallEntityId = tmpEntityId;
MapGamePanel.dragItemXCoord = tempX;
MapGamePanel.dragItemYCoord = tempY;
MapGamePanel.dragItemEntityId = tmpEntityId;
MapGamePanel.dragItemId = tmpId;
MapGamePanel.dragItemAmt = tmploot_amt;
MapGamePanel.draggingItemFilename = tmploot_filename;
}
}
MapGamePanel.draggingItem = true;
Point point = e.getPoint();
MapGamePanel.startX = point.x;
MapGamePanel.startY = point.y;
}
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3){ //right
movementBitKeys.keyReleased(87);
movementBitKeys.keyReleased(68);
movementBitKeys.keyReleased(83);
movementBitKeys.keyReleased(65);
mouseHeld = false;
}
if (MapGamePanel.draggingItem){
int whiteLeftSpace = (theDesktop.getWidth() - xTiles*32)/2;
String[] globalCoords = localToGlobalCoords((MapGamePanel.currentX-whiteLeftSpace)/32 + "," + MapGamePanel.currentY/32).split(",");
int globalX = Integer.parseInt(globalCoords[0]);
int globalY = Integer.parseInt(globalCoords[1]);
String[] startCoords = localToGlobalCoords((MapGamePanel.startX-whiteLeftSpace)/32 + "," + MapGamePanel.startY/32).split(",");
int startX = Integer.parseInt(startCoords[0]);
int startY = Integer.parseInt(startCoords[1]);
if (globalX == characterX && globalY == characterY){
sendToServer("pickupItem|" + startX + "," + startY + "-" + MapGamePanel.dragItemEntityId + "|backpack|-1|" + MapGamePanel.dragItemAmt);
} else if (((globalX-1) == characterX || globalX == characterX || (globalX+1) == characterX) &&
((globalY-1) == characterY || globalY == characterY || (globalY+1) == characterY)){
if (!(startX == globalX && startY == globalY)){
sendToServer("moveItem|" + startX + "," + startY + "-" + MapGamePanel.dragItemEntityId + "|ground|" + globalX + "," + globalY + "|-1|" + MapGamePanel.dragItemAmt);
}
}
MapGamePanel.draggingItem = false;
}
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
和是使用JLayeredPane
的好例子
显示在玻璃窗格上拖动
JComponent#setComponentPopupMenu()
将是添加上下文菜单的一种方式。为了解决这个问题,我创建了一个JLabel,当项目处于我的(DragingItem boolean)状态时,我将始终更新它。下面是一些代码。谢谢大家的帮助和想法
public void mouseDragged(MouseEvent e) {
updateMouseCursor(e);
if (MapGamePanel.draggingItem){
Point point = e.getPoint();
MapGamePanel.currentX = point.x;
MapGamePanel.currentY = point.y;
dragJLabel.setBounds(MapGamePanel.currentX, MapGamePanel.currentY, 32, 32);
}
}
public void mousePressed(MouseEvent e) {
MapGamePanel.startX = point.x;
MapGamePanel.startY = point.y;
dragJLabel = new JLabel(MapGamePanel.draggingItemFilename);
dragJLabel.setBounds(MapGamePanel.startX, MapGamePanel.startY, 32, 32);
theDesktop.add(dragJLabel, JLayeredPane.DRAG_LAYER);
}
对MouseRelease来说也是一样的,我会移除JLabel。你也可以在我的截图中看到,现在还没有图像,只有文本,只是快速编码,这样我就可以在这里提供答案来帮助其他人
看起来拖放是您在这里的最佳选择。这很有意义。。。你能详细说明一下吗?我必须承认我没有拖放代码的经验。可能是一个例子或另一篇文章,我可以阅读?谢谢你的反馈,顺便说一句。我希望我有时间写一个例子。这是一个更复杂的Swing特性,至少对我来说是这样,我会让你参考教程,并在这个网站上搜索示例。这就是我过去为此所做的。我会做的,谢谢你的帮助,从哪里开始:D.我要休息一下,所以如果你以后有时间,请随意写这个例子;)不太可能很快发生。现在的工作责任很重。我试着使用JLayeredPane的概念,但运气不太好。。。现在,我只是想在我的游戏地图上画一个随机的JLabel,但无法让它工作。。我已经发布了上面的代码。。。有什么线索吗?我想你需要为层使用不同的数字,
我通常忘记做
setBounds()`。对于我的游戏,除了拖拽绘画(在本例中使用jlabel测试)之外,所有内容都应该在同一层上。默认图层低于拖动图层(最高)。所以你认为setBounds()就是它不起作用的原因?是的,setBounds()就是问题所在。非常感谢卡塔琳娜。我将此标记为“为您的利益而接受”,然后当我提出我的解决方案时,我将发布我为任何人所做的事情,谁发现此帖子,我将受益。哈哈哈,不,不幸的是,这与我的艺术技能一样好。。。如果需要,请访问我的网站:)www.kisnardonline.com
public void mouseDragged(MouseEvent e) {
updateMouseCursor(e);
if (MapGamePanel.draggingItem){
Point point = e.getPoint();
MapGamePanel.currentX = point.x;
MapGamePanel.currentY = point.y;
dragJLabel.setBounds(MapGamePanel.currentX, MapGamePanel.currentY, 32, 32);
}
}
public void mousePressed(MouseEvent e) {
MapGamePanel.startX = point.x;
MapGamePanel.startY = point.y;
dragJLabel = new JLabel(MapGamePanel.draggingItemFilename);
dragJLabel.setBounds(MapGamePanel.startX, MapGamePanel.startY, 32, 32);
theDesktop.add(dragJLabel, JLayeredPane.DRAG_LAYER);
}