Button b = new Button(); b.addActionListener(new MyActionListener());public class MyActionListener implements ActionListener{ public voic actionPerformed(ActionEvent e) { //do what do you like }}
Java组件本身就已经被添加到了监听序列中,只要为其添加监听者addXxxListener,就可以在组件层次处理鼠标等人机互动事件see:class VisualFigure extends JLabel implements MouseListener, MouseMotionListener { int rank, file; // The field where this figure should be. int mousePressedX, mousePressedY; //Where this figure started // to be dragged. boolean isWhiteFigure; int type; public VisualFigure(int _type) { super((ImageIcon) icons.get(new Integer(_type))); type = _type; isWhiteFigure = type < 7; setSize(getIcon().getIconWidth(), getIcon().getIconHeight()); addMouseMotionListener(this); addMouseListener(this); } public int getRank() { if (reverseBoard) return ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE); else return 7 - ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE); } public int getFile() { if (reverseBoard) return 7 - ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE); else return ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE); } public void setField(int newRank, int newFile) { if (reverseBoard) setLocation((7 - newFile) * FIELDSIZE, newRank * FIELDSIZE); else setLocation(newFile * FIELDSIZE, (7 - newRank) * FIELDSIZE); } // The following methods are required for the MouseListener // and MouseMotionListener interfaces. public void mousePressed(MouseEvent e) { if (isLocked) return; rank = getRank(); file = getFile(); mousePressedX = e.getX(); mousePressedY = e.getY(); synchronized (virtualBoard) { if (virtualBoard.field[(rank << 3) + file] == VirtualBoard.EMPTY_FIELD) { VisualBoard.this.update(); return; } if (jcb.settings.showPossibleMoves) for (int r = 0; r < 8; r++) for (int f = 0; f < 8; f++) if (virtualBoard.isPossibleMove(new Move(rank, file, r, f))) if (reverseBoard) background[r][7 - f].setBackground(jcb.settings.boardHighlightColor); else background[7 - r][f].setBackground(jcb.settings.boardHighlightColor); } } public void mouseDragged(MouseEvent e) { if (isLocked) return; int newX, newY; newX = getLocation().x + e.getX() - mousePressedX; if (newX > MAX) newX = MAX; if (newX < 0) newX = 0; newY = getLocation().y + e.getY() - mousePressedY; if (newY > MAX) newY = MAX; if (newY < 0) newY = 0; setLocation(newX, newY); boardPane.setLayer(this, 10, 0); } public void mouseReleased(MouseEvent e) { if (isLocked) return; synchronized (virtualBoard) { int newRank = getRank(); int newFile = getFile(); boardPane.setLayer(this, 2, 0); if (rank != newRank || file != newFile) { final Move move = new Move(rank, file, newRank, newFile); if (virtualBoard.isPossibleMove(move)) { if ((virtualBoard.field[move.fromField()] == VirtualBoard.WHITE_PAWN && move.toRank() == 7) || (virtualBoard.field[move.fromField()] == VirtualBoard.BLACK_PAWN && move.toRank() == 0)) { move.setPawnPromotion(selectPromotion()); } //isLocked=true; synchronized (jcb) { jcb.makeUsersMove(move); } } else VisualBoard.this.update(); } else VisualBoard.this.update(); } } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } }
b.addActionListener(new MyActionListener());public class MyActionListener implements ActionListener{ public voic actionPerformed(ActionEvent e) {
//do what do you like
}}
int rank, file; // The field where this figure should be.
int mousePressedX, mousePressedY; //Where this figure started
// to be dragged.
boolean isWhiteFigure;
int type; public VisualFigure(int _type) {
super((ImageIcon) icons.get(new Integer(_type)));
type = _type;
isWhiteFigure = type < 7;
setSize(getIcon().getIconWidth(), getIcon().getIconHeight());
addMouseMotionListener(this);
addMouseListener(this);
}
public int getRank() {
if (reverseBoard)
return ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE);
else
return 7 - ((getLocation().y + (FIELDSIZE / 2)) / FIELDSIZE);
}
public int getFile() {
if (reverseBoard)
return 7 - ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE);
else
return ((getLocation().x + (FIELDSIZE / 2)) / FIELDSIZE);
}
public void setField(int newRank, int newFile) {
if (reverseBoard)
setLocation((7 - newFile) * FIELDSIZE, newRank * FIELDSIZE);
else
setLocation(newFile * FIELDSIZE, (7 - newRank) * FIELDSIZE);
} // The following methods are required for the MouseListener
// and MouseMotionListener interfaces.
public void mousePressed(MouseEvent e) {
if (isLocked)
return;
rank = getRank();
file = getFile();
mousePressedX = e.getX();
mousePressedY = e.getY();
synchronized (virtualBoard) {
if (virtualBoard.field[(rank << 3) + file] == VirtualBoard.EMPTY_FIELD) {
VisualBoard.this.update();
return;
}
if (jcb.settings.showPossibleMoves)
for (int r = 0; r < 8; r++)
for (int f = 0; f < 8; f++)
if (virtualBoard.isPossibleMove(new Move(rank, file, r, f)))
if (reverseBoard)
background[r][7 - f].setBackground(jcb.settings.boardHighlightColor);
else
background[7 - r][f].setBackground(jcb.settings.boardHighlightColor);
}
}
public void mouseDragged(MouseEvent e) {
if (isLocked)
return;
int newX, newY;
newX = getLocation().x + e.getX() - mousePressedX;
if (newX > MAX)
newX = MAX;
if (newX < 0)
newX = 0;
newY = getLocation().y + e.getY() - mousePressedY;
if (newY > MAX)
newY = MAX;
if (newY < 0)
newY = 0;
setLocation(newX, newY);
boardPane.setLayer(this, 10, 0);
}
public void mouseReleased(MouseEvent e) {
if (isLocked)
return;
synchronized (virtualBoard) {
int newRank = getRank();
int newFile = getFile();
boardPane.setLayer(this, 2, 0);
if (rank != newRank || file != newFile) {
final Move move = new Move(rank, file, newRank, newFile);
if (virtualBoard.isPossibleMove(move)) {
if ((virtualBoard.field[move.fromField()] == VirtualBoard.WHITE_PAWN && move.toRank() == 7)
|| (virtualBoard.field[move.fromField()] == VirtualBoard.BLACK_PAWN && move.toRank() == 0)) {
move.setPawnPromotion(selectPromotion());
}
//isLocked=true;
synchronized (jcb) {
jcb.makeUsersMove(move);
}
} else
VisualBoard.this.update();
} else
VisualBoard.this.update();
}
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}