Java 调用 Swing MouseListener 类
Java Swing MouseListener Calling classes
需要一些大学作业方面的指导。因此,如果您能给我指点而不是实际的解决方案,我们将不胜感激。
受命修改现有程序,以便当鼠标悬停在草稿板的深色方块上时,只有深色方块会更改为不同的颜色。当鼠标离开黑色方块时,它会变回原来的颜色。
我已经包含了我认为对解决此任务很重要的两个 class。
感谢您提供的任何帮助。一段时间以来,我一直在为这个问题挠头。
部分任务是只编辑 Board class 而不是其他人。
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();
}
}
这里是广场Class
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
的对象)进行比较,而该对象从未被使用过。
您应该直接使用作为事件源的 Square
,例如:
public void mouseExited(MouseEvent e)
{
if (e.getSource() instanceof Square)
{
((Square)e.getSource()).exit();
}
}
需要一些大学作业方面的指导。因此,如果您能给我指点而不是实际的解决方案,我们将不胜感激。
受命修改现有程序,以便当鼠标悬停在草稿板的深色方块上时,只有深色方块会更改为不同的颜色。当鼠标离开黑色方块时,它会变回原来的颜色。
我已经包含了我认为对解决此任务很重要的两个 class。
感谢您提供的任何帮助。一段时间以来,我一直在为这个问题挠头。 部分任务是只编辑 Board class 而不是其他人。
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();
}
}
这里是广场Class
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
的对象)进行比较,而该对象从未被使用过。
您应该直接使用作为事件源的 Square
,例如:
public void mouseExited(MouseEvent e)
{
if (e.getSource() instanceof Square)
{
((Square)e.getSource()).exit();
}
}