gpt4 book ai didi

java - for 循环在第一个循环后无法正常工作

转载 作者:行者123 更新时间:2023-12-02 02:05:02 25 4
gpt4 key购买 nike

我希望使用 Swing 创建一个简单的 TicTacToe GUI 游戏;我创建了名为 TicTacToe 的 JFrame并将 JPanel 放入其中,名为 gamePanelGridLayout(3,3) 。然后我创建了一个名为 buttons[][] 的按钮数组对于我的按钮,我将在其中放置 X 的图标或O 。然后我生成了 9 个按钮添加了逻辑。 JPanel代码:

public class gamePanel extends JPanel {
XOButton buttons[][] = new XOButton[3][3];
boolean isX;
public gamePanel() {
setLayout(new GridLayout(3, 3));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++) {
buttons[i][j] = new XOButton();
buttons[i][j].setIcon(null);
add(buttons[i][j]);
}
}
isX = true;
}

private class XOButton extends JButton {
ImageIcon X, O;

public XOButton() {
X = new ImageIcon(gamePanel.this.getClass().getResource("X.png"));
O = new ImageIcon(gamePanel.this.getClass().getResource("O.png"));
addActionListener(this::actionPerformed);
}

public int getValue(XOButton btn){
if(btn.getIcon().toString().equals(null)){
return 0;
}
if(btn.getIcon().toString().equals(X.toString())){
return 1;
}
if(btn.getIcon().toString().equals(O.toString())){
return 2;
}
return 0;
}

public void actionPerformed(ActionEvent e) {
if(getIcon() != null) return;
if (isX) {
setIcon(X);
isX = false;
checkWinner();
} else {
setIcon(O);
isX = true;
checkWinner();
}
}

public void checkWinner(){
for(int i = 0; i < 3; i++){
if(getValue(buttons[i][0]) != 0 && getValue(buttons[i][0]) == getValue(buttons[i][1])
&& getValue(buttons[i][1]) == (getValue(buttons[i][2]))) {
if (getValue(buttons[i][0]) == 1) {
JOptionPane.showMessageDialog(null, X);
return;
} else if (getValue(buttons[i][0]) == 2) {
JOptionPane.showMessageDialog(null, O);
return;
}
}
}
}
}

}

我想知道我是否可以获得问题的答案(拜托!):如您所见,我创建了名为 checkWinner 的过程但它只是检查按钮图标在一行中是否相等。但是当我使用此代码时,它仅适用于第一行。第二个和第三个被忽略,直到我填充第一行的前两个按钮。当代码运行时,它应该显示带有 X 或 O 图像的消息(取决于谁是获胜者)。 Image where it works fine in the first rowImage where it is not working (until I will fill first row for a half)当我填充第二张图像中的第一行时,它将显示两个 messageDialogsXO图片。

我做错了什么?我也会感谢有关我的代码的答案和评论,这很糟糕,我想提高我的技能。感谢您的关注。

编辑:按下任何按钮时出现此异常:

`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gamePanel$XOButton.getValue(gamePanel.java:34)
at gamePanel$XOButton.checkWinner(gamePanel.java:61)
at gamePanel$XOButton.actionPerformed(gamePanel.java:51)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6589)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6354)
at java.desktop/java.awt.Container.processEvent(Container.java:2260)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2318)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4906)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2304)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

`

最佳答案

在 getValue 中:

btn.getIcon().toString().equals(null)

对于未单击的 Button,btn.getIcon() 为 null。因此,toString() 会引发 NPE。

尝试将其更改为

if(btn.getIcon() != null)

其他 if 也一样

if(btn.getIcon() != null && btn.getIcon().equals(X))

而不是

 if(btn.getIcon().toString().equals(X.toString()))

关于java - for 循环在第一个循环后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50976261/

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