因此,我将图像作为ImageIcon存储在JButton上。我希望用户点击他们想要使用的作品的JButton,然后点击另一个JButton将其移动到那里,我该怎么做?在Java中制作象棋游戏,我想移动棋子
我试过使用actionListener来获取ImageIcon,但其证明非常复杂,特别是因为我有一个JButton图像的2d数组。
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
JButton[][] squares = new JButton[8][8];
Border emptyBorder = BorderFactory.createEmptyBorder();
for (int row = 0; row < squares.length; row++) {
for (int col = 0; col < squares[row].length; col++) {
JButton tempButton = new JButton();
tempButton.setBorder(emptyBorder);
tempButton.setSize(64, 64);
squares[row][col] = tempButton;
squares[row][col].addActionListener(actionListener);
panel.add(squares[row][col]);
squares[0][0].setIcon(new ImageIcon(BoardGUI.class.getResource("castle.png"), "castle"));
}
}
如果你包括了你所拥有的东西到底有什么问题,那么这对人们可能会有帮助。 – Galax