Итак, мои изображения хранятся как ImageIcon на JButtons. Я хочу, чтобы пользователь щелкнул JButton части, которую он хочет использовать, а затем щелкнул другой JButton, чтобы переместить его туда, как мне это сделать?
Я пытался использовать actionListener для получения ImageIcon, но это оказалось очень сложно, особенно потому, что у меня есть двумерный массив изображений JButton.
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"));
}
}