我是一名经验有限的新程序员。这是我的第一个GUI设计。我正在尝试创建一个带有移动图像图标的国际象棋GUI,我将在桌面坐标上点击鼠标,这是第一次和第二次按下的。但问题是,即使我定义了鼠标侦听器方法,这些方法源自MouseListener接口,我无法从鼠标的移动中获得合适的坐标。我查看了MouseEvent对象的getX和getY方法的值,并在下面看到:
mouseEntered (7,3)
mouseExited (3,77)
mouseEntered (3,15)
mouseExited (-1,55)
mouseEntered (61,55)
mouseExited (61,74)
mouseEntered (61,12)
mouseExited (63,33)
mouseEntered (1,33)
mouseExited (6,75)
mouseEntered (6,13)
mouseExited (18,68)
mouseEntered (18,6)
mouseExited (30,70)
mouseEntered (30,8)
mouseExited (62,43)
mouseEntered (0,43)
mouseExited (51,-5)
mouseEntered (51,57)
mouseExited (62,45)
mouseEntered (0,45)
mouseExited (63,17)
mouseEntered (1,17)
mouseExited (32,64)
mouseEntered (32,2)
mouseExited (61,64)
mouseEntered (61,2)
mouseExited (63,15)
mouseEntered (1,15)
mouseExited (-1,25)
mouseEntered (61,25)
mouseExited (-6,49)
mouseEntered (56,49)
mouseExited (-13,50)
mouseEntered (49,50)
mouseExited (-10,34)
mouseEntered (52,34)
mouseExited (8,-6)
mouseEntered (8,56)
mouseExited (1,-7)
mouseEntered (1,55)
mouseExited (13,-4)
mouseEntered (13,58)
mouseExited (57,-1)
mouseEntered (57,61)
mouseExited (62,59)
mouseEntered (0,59)
mouseExited (62,40)
mouseEntered (0,40)
mouseExited (33,-1)
mouseEntered (33,61)
mouseExited (-3,12)
mouseEntered (59,12)
mouseExited (28,-1)
mouseEntered (28,61)
mouseExited (-1,55)
mouseEntered (61,55)
mouseExited (-2,43)
mouseEntered (60,43)
mouseExited (-2,45)
mouseEntered (60,45)
mouseExited (9,62)
mouseEntered (9,0)
mouseExited (62,25)
mouseEntered (0,25)
mouseExited (64,42)
mouseEntered (2,42)
mouseExited (35,64)
mouseEntered (35,2)
mouseExited (62,50)
mouseEntered (0,50)
mouseExited (4,65)
mouseEntered (4,3)
mouseExited (21,63)
mouseEntered (21,1)
mouseExited (47,62)
mouseEntered (47,0)
mouseExited (65,6)
mouseEntered (3,6)
mouseExited (14,-13)
mouseEntered (14,49)
mouseExited (42,-17)
mouseEntered (42,45)
mouseExited (63,-2)
mouseEntered (1,60)
mouseExited (47,64)
mouseEntered (47,2)
mouseExited (63,24)
mouseEntered (1,24)
mouseExited (62,47)
mouseEntered (0,47)
mouseExited (65,24)
虽然我有8x8网格布局,但我得到了这些坐标。为什么我会得到奇怪的坐标?
此外,我试图通过鼠标监听器更改图像图标的位置。由于得到奇怪的坐标我无能为力。代码如下(没有鼠标监听器方法):
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame implements MouseListener {
private Board auxBoard = new Board(); //8x8 String chess table class
private JPanel[][] squares = new JPanel[8][8];
private JPanel mainPanel = new JPanel(new GridLayout(8, 8));
private Container c;
private String[][] chessBoard = auxBoard.strChessBoard; //invoke chess table on which game will be played
private ImageIcon rookBlack = new ImageIcon(System.getProperty("user.dir")
+ "/icons/br.png");
private ImageIcon rookWhite = new ImageIcon(System.getProperty("user.dir")
+ "/icons/wr.png");
private ImageIcon bishopBlack = new ImageIcon(
System.getProperty("user.dir") + "/icons/bb.png");
private ImageIcon bishopWhite = new ImageIcon(
System.getProperty("user.dir") + "/icons/wb.png");
private ImageIcon knightBlack = new ImageIcon(
System.getProperty("user.dir") + "/icons/bkn.png");
private ImageIcon knightWhite = new ImageIcon(
System.getProperty("user.dir") + "/icons/wkn.png");
private ImageIcon kingBlack = new ImageIcon(System.getProperty("user.dir")
+ "/icons/bk.png");
private ImageIcon kingWhite = new ImageIcon(System.getProperty("user.dir")
+ "/icons/wk.png");
private ImageIcon queenBlack = new ImageIcon(System.getProperty("user.dir")
+ "/icons/bq.png");
private ImageIcon queenWhite = new ImageIcon(System.getProperty("user.dir")
+ "/icons/wq.png");
private ImageIcon pawnBlack = new ImageIcon(System.getProperty("user.dir")
+ "/icons/bp.png");
private ImageIcon pawnWhite = new ImageIcon(System.getProperty("user.dir")
+ "/icons/wp.png");
public GUI() {
c = getContentPane();
setBounds(100, 100, 510, 530);
setBackground(new Color(204, 204, 204));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Tittle");
setResizable(false);
c.setLayout(null);
mainPanel.setBounds(3, 3, 500, 500);
mainPanel.setBackground(new Color(255, 255, 255));
c.add(mainPanel);
this.drawChessBoard();
this.arrangeChessPieces();
setVisible(true);
}
private void drawChessBoard(){
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++){
squares[y][x] = new JPanel(new BorderLayout());
squares[y][x].addMouseListener(this);
mainPanel.add(squares[y][x]);
if (y % 2 == 0)
if (x % 2 != 0)
squares[y][x].setBackground(Color.GRAY);
else
squares[y][x].setBackground(Color.CYAN);
else
if (x % 2 == 0)
squares[y][x].setBackground(Color.GRAY);
else
squares[y][x].setBackground(Color.CYAN);
}
}
private void arrangeChessPieces() {
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++) {
this.squares[y][x].add(
this.getPieceObject(chessBoard[y][x]),
BorderLayout.CENTER);
this.squares[y][x].validate();
}
}
private JLabel getPieceObject(String strPieceName) {
JLabel lblTemp;
if (strPieceName.equals("RB"))
lblTemp = new JLabel(this.rookBlack);
else if (strPieceName.equals("BB"))
lblTemp = new JLabel(this.bishopBlack);
else if (strPieceName.equals("NB"))
lblTemp = new JLabel(this.knightBlack);
else if (strPieceName.equals("QB"))
lblTemp = new JLabel(this.queenBlack);
else if (strPieceName.equals("KB"))
lblTemp = new JLabel(this.kingBlack);
else if (strPieceName.equals("PB"))
lblTemp = new JLabel(this.pawnBlack);
else if (strPieceName.equals("RW"))
lblTemp = new JLabel(this.rookWhite);
else if (strPieceName.equals("BW"))
lblTemp = new JLabel(this.bishopWhite);
else if (strPieceName.equals("NW"))
lblTemp = new JLabel(this.knightWhite);
else if (strPieceName.equals("QW"))
lblTemp = new JLabel(this.queenWhite);
else if (strPieceName.equals("KW"))
lblTemp = new JLabel(this.kingWhite);
else if (strPieceName.equals("PW"))
lblTemp = new JLabel(this.pawnWhite);
else
lblTemp = new JLabel();
return lblTemp;
}
public void mouseClicked(MouseEvent e) {
System.out.println("mousePressed (" + e.getX() + "," + e.getY() + ")");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed (" + e.getX() + "," + e.getY() + ")");
repaint();
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased (" + e.getX() + "," + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered (" + e.getX() + "," + e.getY() + ")");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited (" + e.getX() + "," + e.getY() + ")");
}
public static void main(String args[]) {
GUI test = new GUI();
}
}
我怎样才能使这些坐标更容易使用?另外,我怎么能移动棋子在桌子上移动并将他们的第一个和最后一个位置作为x,y坐标? 任何帮助表示赞赏。谢谢。
答案 0 :(得分:0)
更简单的方法是使用代表Board GUI的JPanel
,并且它有一个成员字段,该字段是JPanel
的数组,每个元素都是电路板的图块。然后你可以用每个图块(应该知道它的坐标)注册事件,而不必担心翻译鼠标坐标。
您可以在github上查看我的国际象棋游戏,以供参考,具体来说:
https://github.com/amir650/chess/blob/master/code/com/chess/view/GuiTable.java
答案 1 :(得分:0)
查看MouseEvent.getY()的文档会产生:
返回事件相对于源组件的垂直y位置。
getX()和getY()与您的棋盘方块没有任何关系,但返回的是像素值。更准确地说,是来自源的y或x方向的距离。
编辑:你似乎选择了单个正方形作为面板。我只想为每个面板分配一个单独的监听器。因此,当触发事件时,相应的侦听器可以告诉您它已被选中。