我目前正在尝试为名为 Carcassone 的游戏创建应用程序,但在将 JLayeredPane 与 MouseAdapters 结合使用时遇到问题。
目前我有一个 JLayeredPane,其中包含一个带有 GridLayout 的 JPanel,用作游戏板。GridLayout 的每个单元格都包含一个 JPanel,其中包含一个 JLabel 来表示板上的图块。(空图块只是一个带有黑色方块的 JLabel 作为图像)。
我在 JLayeredPane 中添加了一个自定义鼠标适配器 (TileMouseAdapter),它允许用户在游戏板上移动图块(鼠标按下 - 拖动 - 释放)或旋转图块(鼠标单击)。(要测试单击下一个图块按钮并在出现的图块周围移动,单击提交按钮后,如果它是有效移动,则图块将不再可拖动)。在我添加下一部分之前,这一切都可以正常工作。
现在我想在屏幕顶部的 JLayeredPane 中添加一个单独的 JPanel。该部分应包含玩家可以放置在瓷砖上的游戏棋子。我还希望 JPanel 保存游戏棋子,以便玩家可以将棋子拖到图块上。但是,当我创建此面板并将其添加到 JLayeredPane 时,上一节中的鼠标适配器不再正常工作。例如,如果您单击磁贴的底部,则它不再识别您正在单击该磁贴。如果您单击每个图块的上半部分,它只会选择该图块。此外,如果您单击一个图块的底部,有时它会选择下面的图块。即,似乎选择图块的机制似乎向上移动了大约半个图块。任何人都可以找出这个问题的解释/可能的解决方法吗?谢谢!
下面的代码是我用来创建 JLayeredPane 的代码,该 JLayeredPane 包含用于棋盘 (myBoardPanel) 的 JPanel 以及用于游戏部件的面板 (makeMeeplePanel())
private JLayeredPane makeBoardPanel() {
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(new BorderLayout());
myBoardPanel = new CarcassoneBoardPanel(layeredPane);
layeredPane.add(makeMeeplePanel(), BorderLayout.NORTH, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(myBoardPanel, BorderLayout.CENTER, JLayeredPane.DEFAULT_LAYER);
TileMouseAdapter tileMouseAdapter = new TileMouseAdapter(layeredPane);
//MeepleMouseAdapter meepleMouseAdapter = new MeepleMouseAdapter(layeredPane);
layeredPane.addMouseListener(tileMouseAdapter);
layeredPane.addMouseMotionListener(tileMouseAdapter);
//layeredPane.addMouseListener(meepleMouseAdapter);
//layeredPane.addMouseMotionListener(meepleMouseAdapter);
return layeredPane;
}
这是我为游戏板定制的鼠标适配器:
package gui;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import tiles.*;
public class TileMouseAdapter extends MouseAdapter {
private JLayeredPane myLayeredPane;
private CarcassoneBoardPanel myBoard;
private Tile dragLabel;
private JPanel clickedPanel;
public TileMouseAdapter(JLayeredPane layeredPane) {
myLayeredPane = layeredPane;
myBoard = (CarcassoneBoardPanel) layeredPane.getComponents()[1];
}
private void reset() {
if (dragLabel != null) {
myLayeredPane.remove(dragLabel);
myLayeredPane.revalidate();
myLayeredPane.repaint();
}
dragLabel = null;
clickedPanel = null;
}
@Override
public void mouseClicked(MouseEvent event) {
clickedPanel = (JPanel) myBoard.getComponentAt(event.getPoint());
if (clickedPanel == null) {
reset();
return;
}
Component[] components = clickedPanel.getComponents();
if (!(components[0] instanceof Tile) || components[0] instanceof EmptyTile || !((Tile) components[0]).isDraggable()) {
return;
}
dragLabel = (Tile) components[0];
dragLabel.rotate();
clickedPanel.revalidate();
clickedPanel.repaint();
reset();
}
@Override
public void mousePressed(MouseEvent event) {
clickedPanel = (JPanel) myBoard.getComponentAt(event.getPoint());
System.out.println(event.getPoint());
if (clickedPanel == null) {
reset();
return;
}
Component[] components = clickedPanel.getComponents();
if (!(components[0] instanceof Tile) || (components[0] instanceof EmptyTile) || !((Tile) components[0]).isDraggable()) {
return;
}
dragLabel = (Tile) components[0];
clickedPanel.remove(dragLabel);
clickedPanel.add(new EmptyTile(dragLabel.getRow(), dragLabel.getCol()));
clickedPanel.revalidate();
clickedPanel.repaint();
int x = event.getPoint().x - dragLabel.getWidth() / 2;
int y = event.getPoint().y - dragLabel.getHeight() / 2;
dragLabel.setLocation(x, y);
try {
myLayeredPane.add(dragLabel, JLayeredPane.DRAG_LAYER);
myLayeredPane.repaint();
} catch (IllegalArgumentException e) {
//TODO: deal with this?
//gives error for some unknown reason, but doesnt effect anything? ignore...dumb error cus jswing sucks
}
}
@Override
public void mouseDragged(MouseEvent event) {
if (dragLabel == null) {
return;
}
int x = event.getPoint().x - dragLabel.getWidth() / 2;
int y = event.getPoint().y - dragLabel.getHeight() / 2;
dragLabel.setLocation(x, y);
myLayeredPane.revalidate();
}
@Override
public void mouseReleased(MouseEvent event) {
//TODO: make sure cannot place tile on already occupied panel...
if (dragLabel == null || !dragLabel.isDraggable()) {
return;
}
myLayeredPane.remove(dragLabel);
myLayeredPane.revalidate();
myLayeredPane.repaint();
JPanel droppedPanel = (JPanel) myBoard.getComponentAt(event.getPoint());
if (droppedPanel == null) {
clickedPanel.removeAll();
clickedPanel.add(dragLabel);
clickedPanel.revalidate();
reset();
return;
}
dragLabel.setPosition(((Tile) droppedPanel.getComponents()[0]).getRow(), ((Tile) droppedPanel.getComponents()[0]).getCol());
droppedPanel.removeAll();
droppedPanel.add(dragLabel);
droppedPanel.revalidate();
reset();
}
}
如果您需要更多信息/代码,请告诉我。这是我第一次使用这个网站,如果我没有提供足够的解释,非常抱歉,等等......谢谢!