需要一些關於大學作業的指導。所以,如果你能給我指針,而不是真正的解決方案,將不勝感激。Java Swing MouseListener調用類
一直負責修改現有的程序,以便當鼠標懸停在草稿板的黑色方塊上時,只有黑色方塊變爲不同的顏色。當鼠標退出黑色方塊時,它會回到原來的顏色。
我已經包括了兩個班,我認爲這對解決這個任務很重要。
謝謝你收到的任何幫助。現在一段時間以來我一直在抓這個名字。 任務的一部分是僅編輯委員會類而不編輯其他人。
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);
}
}
已運行/調試的代碼?它是否像你期望的那樣工作? – efekctive
嗨,感謝您的快速回復。 代碼創建了一個草稿板,但是當我將鼠標懸停在黑色塊上時,它們不會變爲黃色。 我知道我可能在這裏提出一些簡單的錯誤,所以提前抱歉! – RiceCrispy
放置一個斷點並逐步完成代碼。這將告訴你,如果檢查黑暗作品 – efekctive