首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java调用类

Java调用类
EN

Stack Overflow用户
提问于 2017-04-05 15:49:11
回答 1查看 71关注 0票数 0

需要一些关于大学作业的指导。所以,如果你能给我指点,而不是实际的解决方案,那将是非常感谢的。

任务是修改现有程序,以便当鼠标悬停在绘图板的黑暗方块上时,只有黑暗方块被更改为不同的颜色。当鼠标离开黑暗的方块时,它会回到原来的颜色。

我已经包括了两门课,我觉得这两门课对解决这个任务很重要。

谢谢你的帮助。我在这个问题上已经抓了一段时间了。部分任务是只编辑Board类,而不编辑其他类。

代码语言:javascript
运行
复制
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的空方法,尽管我们还没有学到这一点。我还没有在这段代码中包含未使用的方法。

代码语言:javascript
运行
复制
public void mouseExited(MouseEvent e) 
{
if (e.getSource().equals(square))
{
 square.exit();
}
}

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

这里是广场班

代码语言:javascript
运行
复制
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);
 }


 }   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-05 15:59:20

您总是比较同一个Square对象(称为square),它从未被使用过。

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

代码语言:javascript
运行
复制
public void mouseExited(MouseEvent e) 
{
    if (e.getSource() instanceof Square)
    {
         ((Square)e.getSource()).exit();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43235973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档