我为国际象棋应用程序创建了一个桌面窗格和内部框架。外窗具有新游戏等菜单。它非常棒。但我面临的问题是内部框架中的鼠标事件。因为我的内部框架是我的棋盘棋子。当我没有使用具有内部框架的桌面窗格时,鼠标事件正在工作,这只是游戏的简单框架方法。当我将代码切换为使用带有内部框架的桌面窗格时,我的鼠标事件停止工作。
我希望我的问题对每个人都清楚,并希望得到一个有用的答案。 这是源代码:
frame
是国际象棋游戏所在的内部框架。桌面窗格应位于框架上方,并具有新游戏的菜单栏并退出。
public void createFrame() {
JInternalFrame frame = new JInternalFrame();
这是我的棋盘
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
Dimension boardSize = new Dimension(400, 400);
//FlowLayout flow=new FlowLayout();
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
chessBoard.add(square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.LIGHT_GRAY : Color.RED);
} else {
square.setBackground(i % 2 == 0 ? Color.RED : Color.LIGHT_GRAY);
}
}
棋盘结束了。这些是我的国际象棋pices,其中包含放置在要移动的棋盘上的棋子图像
JLabel piece;
piece = new JLabel( new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects
\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-
of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg") );
JPanel panel = (JPanel)chessBoard.getComponent(0);
panel.add(piece);
piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects
\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-
of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));
panel = (JPanel)chessBoard.getComponent(15);
panel.add(piece);
piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects
\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-
of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));
panel = (JPanel)chessBoard.getComponent(16);
panel.add(piece);
piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects
\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-
of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));
panel = (JPanel)chessBoard.getComponent(20);
panel.add(piece); chess pieces code ended)
frame.setVisible(true);
desktop.add(frame);
frame.setBounds(20,20,410,450);
frame.add(layeredPane);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
//Quit the application.
public void quit() {
System.exit(0);
}
@Override
当代码未转换为内部框架并使用桌面窗格时,这些鼠标事件运行良好...
public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel)
return;
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
@Override
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) return;
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
@Override
public void mouseReleased(MouseEvent e) {
if(chessPiece == null) return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e) {
}
public void BackgroundImageJFrame()
{
}
Displaying the frame
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
Game frame = new Game();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
主要方法&gt;&gt;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}