Java Swing MouseListener 调用类

标签 java swing mouselistener

需要一些关于大学作业的指导。因此,如果您能给我指点而不是实际的解决方案,我将不胜感激。

负责修改现有程序,以便当鼠标悬停在草稿板的黑色方 block 上时,只有黑色方 block 会更改为不同的颜色。当鼠标离开黑色方 block 时,它会恢复到原来的颜色。

我已经包含了我认为对于解决此任务很重要的两个类。

感谢您收到的任何帮助。我已经在这个问题上摸索了一段时间了。 任务的一部分是仅编辑 Board 类,而不编辑其他类。

public class Board extends JPanel implements MouseListener
{
private Square square;
private Square[][] squares;
private ArrayList<DraughtsPiece> lightPieces;
private ArrayList<DraughtsPiece> darkPieces;

public Board(int numRows)
{
    super(new GridLayout(numRows, numRows));
    squares = new Square[numRows][numRows];
    lightPieces = new ArrayList<DraughtsPiece>();
    darkPieces = new ArrayList<DraughtsPiece>();

    setupBoard(numRows);
    setupPieces(numRows);

    allocatePieces();
}

private void setupBoard(int numRows)
{

    boolean lightSquare = true;

    for (int row = 0; row < numRows; row++)
    {
        for (int col = 0; col < numRows; col++)
        {
            if (lightSquare)
            {
                squares[row][col] = new Square(Square.LIGHT_SQUARE_COLOUR, 
row + 1, col + 1);
            }
            else
            {

                squares[row][col] = new Square(Square.DARK_SQUARE_COLOUR, 
row + 1, col + 1);
                addMouseListener(this);
            }
            add(squares[row][col]);
            lightSquare = !lightSquare;
        }
        lightSquare = !lightSquare;
    }
}

private void setupPieces(int numRows)
{
    int numPieces = ((numRows * numRows) - (2 * numRows)) / 4;
    for (int i = 0; i < numPieces; i++)
    {
        DraughtsPiece p = new 
  DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
        lightPieces.add(p);

        p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
        darkPieces.add(p);

    }
}

private void allocatePieces()
{
    int row = squares.length - 1;
    int col = 0;

    for (DraughtsPiece p : lightPieces)
    {
        squares[row][col].setPiece(p);
        col += 2;
        if (col >= squares[0].length)
        {
            col = row % 2;
            row--;
        }
    }

    row = 0;
    col = squares[0].length - 1;
    for (DraughtsPiece p : darkPieces)
    {
        squares[row][col].setPiece(p);
        col -= 2;
        if (col < 0)
        {
            row++;
            col = squares[0].length - 1 - (row % 2);
        }
    }
}

我知道鼠标适配器可用于删除 MouseListener 的空方法,尽管我们还没有被告知这一点。 我没有在此代码中包含未使用的方法。

public void mouseExited(MouseEvent e) 
{
if (e.getSource().equals(square))
{
 square.exit();
}
}

public void mouseEntered(MouseEvent e) 
{
if (e.getSource().equals(square))
{
 square.enter();
}
}

这是 Square 类

public class Square extends JPanel
{
public static final Color LIGHT_SQUARE_COLOUR = new Color(0xdfdfdf);
public static final Color DARK_SQUARE_COLOUR = new Color(0x333333);
public static final Color SELECTED_DARK_SQUARE_COLOUR = Color.YELLOW;

private Color background;
private int row, column;
private Color selectedBackground;
private DraughtsPiece piece;
private boolean selected = false;

public Square(Color c, int row, int col)
{
    super(new BorderLayout());
    background = c;
    this.row = row;
    this.column = col;
    setBackground(background);
    if (background == DARK_SQUARE_COLOUR)
    {
        selectedBackground = SELECTED_DARK_SQUARE_COLOUR;
    }
    piece = null;
 }

 public int getRow()
 {
    return row;
}

public int getColumn()
{
    return column;
}

public void setPiece(DraughtsPiece piece)
{
    if (piece == null && this.piece != null)
    {
        remove(this.piece);
        this.piece.setSquare(null);
        this.piece = null;
    }
    else if (piece != null && this.piece == null)
    {
        this.piece = piece;
        piece.setSquare(this);
        add(piece);
    }
 }

 public DraughtsPiece getPiece()
 {
    return piece;
 }

 protected void enter()
 {
    setBackground(selectedBackground);
 }

 protected void exit()
 {
    setBackground(background);
 }

 protected void setSelected(boolean b)
 {
    selected = b;
    setBackground(b == false ? background : selectedBackground);
 }


 }   

最佳答案

您总是与从未使用过的同一个 Square 对象(称为 square)进行比较。

Youi 应该直接使用作为事件源的 Square,例如:

public void mouseExited(MouseEvent e) 
{
    if (e.getSource() instanceof Square)
    {
         ((Square)e.getSource()).exit();
    }
}

关于Java Swing MouseListener 调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43235973/

相关文章:

java - 匹配正则表达式时出现 java.lang.StackOverflowError

Java 抛出 java.io.FileNotFoundException

java - 我无权访问代码中的图像(R.drawable ...)

Java 计算器显示不正确的结果

java - 如何在 Swing Java 中设置图像的 Z 顺序

java - 如何时刻维护后面的主JFrame?

java - 将最终变量传递给 MouseListener 方法并返回结果

java - 在缓冲图像上的 JPanel 上用鼠标绘制非重叠的 n 边多边形、圆形和椭圆形

java - MouseListener 被另一个 JPanel 组件掩盖了

java - 通过java代码从strings.xml将文本设置为textView时出错