这样的棋盘中,如何实现棋盘的格子随鼠标移动而高亮public ChessGameFrame(int width, int height) { setTitle("斗兽棋"); //设置标题 this.WIDTH = width; this.HEIGTH = height; this.ONE_CHESS_SIZE = (HEIGTH * 4 / 5) / 9; setSize(WIDTH, HEIGTH); setLocationRelativeTo(null); // Center the window. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置程序关闭按键,如果点击右上方的叉就游戏全部关闭了 setLayout(null); addRestart(); addGiveUp(); addUndo(); addChessboard(); addLabel(); addLoadButton(); addSave(); addAImode(); addAImodeGreedy(); addBackground(); }public CellComponent(Color background, Point location, int size) { setLayout(new GridLayout(1,1)); setLocation(location); setSize(size, size); this.background = background; } @Override protected void paintComponent(Graphics g) { super.paintComponents(g); g.setColor(background); g.fillRect(1, 1, this.getWidth()-1, this.getHeight()-1); } }
时间: 2024-01-04 12:03:02 浏览: 30
C#模拟鼠标操作(移动 单击 双击等很多操作)
3星 · 编辑精心推荐
可以通过添加鼠标监听器来实现棋盘的格子随鼠标移动而高亮。具体实现步骤如下:
1. 在 CellComponent 类中添加鼠标监听器:
```
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
// 鼠标进入时设置高亮颜色
background = HIGHLIGHT_COLOR;
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
阅读全文