gpt4 book ai didi

java - 记录 JButton 网格中最后按下的 JButton

转载 作者:行者123 更新时间:2023-11-29 06:29:08 29 4
gpt4 key购买 nike

大家好,我正在尝试使用 Java 创建一个简单的国际象棋游戏。我有一个 [8][8] 数组的 JButton 板。我为每个分配一个新的 SquareListener,它是一个扩展 BoardListner 的类,BoardListner 是一个实现 Action Listener 的父类(super class)。我想记录当前按下的按钮是否等于最后按下的按钮。我正在存储在父类(super class)中按下的 JButton 实例,并在 SqaureListener actionPerformed 方法中引用它。这是我为按钮创建的类的示例。

public class SquareListener extends BoardListener {
private int row;
private int column;


public SquareListener(int row, int column){
this.setRow(row);
this.setColumn(column);
}

@Override
public void actionPerformed(ActionEvent ae){
JButton buttonPressed = (JButton) ae.getSource();
if(buttonPressed == super.lastButtonPressed){
System.out.println("This is the button you last pressed");
}else{
System.out.println("This is a new button");
}
super.lastButtonPressed = buttonPressed;
}

这是我存储 isSelecting 数据的父类(super class)

public class BoardListener implements ActionListener {

private boolean isSelectingInsteadOfTargeting = true;
JButton lastButtonPressed = new JButton();

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}

public boolean isSelectingInsteadOfTargeting() {
return isSelectingInsteadOfTargeting;
}

public void setSelectingInsteadOfTargeting(boolean isSelectingInsteadOfTargeting) {
this.isSelectingInsteadOfTargeting = isSelectingInsteadOfTargeting;
}

}

我希望每次按下按钮时将该按钮的实例传递给 buttonPressed 并与存储在 BoardListener 中的内容进行比较,然后将其打印出来,然后将 buttonPressed 分配给 super 成员。相反,我得到的行为是,如果我按下一个按钮,它将始终返回“这是你上次按下的按钮”,即使我在两者之间按下了一个新按钮。我这样做的方式有问题吗?有没有更简单的方法?

这是我存储按钮并为它们分配 Action 监听器的附加代码

public Board(boolean _isWhite){
gui = new JPanel(new GridLayout(8,8));
chessBoardSquares = new Square[8][8];
boolean shouldBeWhite = true;
for(int i = 0; i < 8; i++){
for(int j= 0; j < 8; j++){
Square square = new Square(i,j);
square.getButton().addActionListener(new SquareListener(i,j));
if(shouldBeWhite){
square.getButton().setBackground(Color.WHITE);
shouldBeWhite = false;
}else{
square.getButton().setBackground(Color.BLACK);
shouldBeWhite = true;
}
if (j == 7){
shouldBeWhite = !shouldBeWhite;
}
chessBoardSquares[i][j] = square;
gui.add(chessBoardSquares[i][j].getButton());
}
}
BoardFactory boardFactory = new BoardFactory();
if(_isWhite){
updateBoardIconsBasedOnCurrentBoardArray(boardFactory.getBlackStartArray());
}else{
//getblack start array and invoke method to change button icons based on array
}
}

最佳答案

我们不需要 8 x 8 的整个网格,但可能需要一个 2 x 2 的网格,您可以将其缩放为 8 x 8。

此代码比较所点击按钮的最后坐标。

您可以为所有按钮使用单个 ActionListener...

例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TwiceButtonPressed {

private JFrame frame;

private JButton[][] buttons;

private JLabel label;
private JLabel sameButtonLabel;

private JPanel pane;

private boolean sameButtonPressed;

private static final int rows = 2;
private static final int cols = 2;

private int buttonRow = -1;
private int buttonCol = -1;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TwiceButtonPressed().createAndShowGui());
}

public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

sameButtonPressed = false;

label = new JLabel("Button pressed: NONE");
sameButtonLabel = new JLabel("Same button pressed: " + sameButtonPressed);

pane = new JPanel();
pane.setLayout(new GridLayout(rows, cols));

buttons = new JButton[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
buttons[i][j] = new JButton(i + "" + j);
buttons[i][j].addActionListener(listener);
pane.add(buttons[i][j]);
}
}

frame.add(sameButtonLabel, BorderLayout.NORTH);
frame.add(pane, BorderLayout.CENTER);
frame.add(label, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (e.getSource().equals(buttons[i][j])) {
label.setText("Button pressed: " + buttons[i][j].getText());
System.out.println(buttonRow + "==" + i + "***" + buttonCol + "==" + j);
if (buttonRow == i && buttonCol == j) {
sameButtonPressed = true;
} else {
sameButtonPressed = false;
}
sameButtonLabel.setText("Same button pressed: " + sameButtonPressed);
buttonRow = i;
buttonCol = j;
}
}
}
}
};
}

enter image description here

此外,您可以将白/黑算法更改为如下内容:

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
buttons[i][j] = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
buttons[i][j].addActionListener(listener);
if (i % 2 == 0) {
if (j % 2 == 0) {
buttons[i][j].setBackground(Color.BLACK);
} else {
buttons[i][j].setBackground(Color.WHITE);
}
} else {
if (j % 2 != 0) {
buttons[i][j].setBackground(Color.BLACK);
} else {
buttons[i][j].setBackground(Color.WHITE);
}
}
pane.add(buttons[i][j]);
}
}

enter image description here

关于java - 记录 JButton 网格中最后按下的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589917/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com