java - 数字拼图编码

原文 标签 java swing

我正在编写数字益智游戏。我有9个按钮,它们全部都包含在一个框架中。我的想法是:按下按钮时,它将检查周围的所有按钮。如果有一个不可见的按钮,则该不可见的按钮将与按下按钮上的数字一起显示,并且被按下的按钮将变为不可见。这就是我所拥有的:

 private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    if (!Btn2.isVisible()) {
        Btn2.setText(Btn1.getText());
        Btn2.setVisible(true);
        Btn1.setVisible(false);
    } else if (!Btn4.isVisible()) {
        Btn4.setVisible(true);
        Btn4.setVisible(false);
        Btn1.setVisible(false);
    }
}                                    

private void Btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if (!Btn3.isVisible()) {
        Btn3.setText(Btn2.getText());
        Btn2.setVisible(false);
        Btn3.setVisible(true);
    } else if (!Btn1.isVisible()) {
        Btn1.setText(Btn2.getText());
        Btn1.setVisible(true);
        Btn2.setVisible(false);
    } else if (!Btn5.isVisible()) {
        Btn5.setText(Btn2.getText());
        Btn5.setVisible(true);
        Btn2.setVisible(false);
    } 
}                                    

private void Btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    if (!Btn2.isVisible()) {
        Btn2.setText(Btn3.getText());
        Btn3.setVisible(false);
        Btn2.setVisible(true);
    } else if (!Btn6.isVisible()) {
        Btn6.setText(Btn3.getText());
        Btn3.setVisible(false);
        Btn6.setVisible(true);
    }
}

 private void Btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn1.isVisible()) {
        Btn1.setText(Btn4.getText());
        Btn1.setVisible(true);
        Btn4.setVisible(false);
    } else if (!Btn5.isVisible()) {
        Btn5.setText(Btn4.getText());
        Btn5.setVisible(true);
        Btn4.setVisible(false);
    } else if (!Btn7.isVisible()) {
        Btn7.setText(Btn4.getText());
        Btn7.setVisible(true);
        Btn4.setVisible(false);
    }
}                                    

private void Btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn2.isVisible()) {
        Btn2.setText(Btn5.getText());
        Btn2.setVisible(true);
        Btn5.setVisible(false);
    }else if (!Btn4.isVisible()) {
        Btn4.setText(Btn5.getText());
        Btn4.setVisible(true);
         Btn5.setVisible(false);
    }else if (!Btn6.isVisible()) {
        Btn6.setText(Btn5.getText());
        Btn6.setVisible(true);
         Btn5.setVisible(false);
    }else if (!Btn8.isVisible()) {
        Btn8.setText(Btn5.getText());
        Btn8.setVisible(true);
         Btn5.setVisible(false);
    }
}                                    

  private void Btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn3.isVisible()) {
        Btn3.setText(Btn6.getText());
        Btn3.setVisible(true);
        Btn6.setVisible(false);
    }else if (!Btn5.isVisible()) {
        Btn5.setText(Btn6.getText());
        Btn5.setVisible(true);
        Btn6.setVisible(false);
    }else if (!Btn9.isVisible()) {
        Btn9.setText(Btn6.getText());
        Btn9.setVisible(true);
        Btn6.setVisible(false);
    }
}                                    

private void Btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn4.isVisible()) {
        Btn4.setText(Btn7.getText());
        Btn4.setVisible(true);
        Btn7.setVisible(false);
    }else if (!Btn8.isVisible()) {
        Btn8.setText(Btn7.getText());
        Btn8.setVisible(true);
        Btn8.setVisible(false);
    }
}                                    

  private void Btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn7.isVisible()) {
        Btn7.setText(Btn8.getText());
        Btn7.setVisible(true);
        Btn8.setVisible(false);
    }else if (!Btn5.isVisible()) {
        Btn5.setText(Btn8.getText());
        Btn5.setVisible(true);
        Btn8.setVisible(false);
    }else if (!Btn9.isVisible()) {
        Btn9.setText(Btn8.getText());
        Btn9.setVisible(true);
        Btn8.setVisible(false);
    }
}                                    

private void Btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    if (!Btn6.isVisible()) {
        Btn6.setText(Btn9.getText());
        Btn6.setVisible(true);
        Btn9.setVisible(false);
    }else if (!Btn8.isVisible()) {
        Btn8.setText(Btn9.getText());
        Btn8.setVisible(true);
        Btn9.setVisible(false);
    }
}                                                                        


似乎很长而且效率很低。我该如何缩短时间?

最佳答案

尝试这个。

static void BtnAction(Button pressed, Button... neighbors) {
    for (Button neighbor : neighbors) {
        if (!neighbor.isVisible()) {
            neighbor.setText(pressed.getText());
            neighbor.setVisible(true);
            pressed.setVisible(false);
            break;
        }
    }
}

private void Btn1ActionPerformed(java.awt.event.ActionEvent evt) {
    BtnAction(Btn1, Btn2, Btn4);
}

private void Btn2ActionPerformed(java.awt.event.ActionEvent evt) {
    BtnAction(Btn2, Btn3, Btn1, Btn5);
}

private void Btn3ActionPerformed(java.awt.event.ActionEvent evt) {
    BtnAction(Btn3, Btn2, Btn6);
}

// and so on...

关于java - 数字拼图编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470232/

相关文章:

java - java中的自定义线程调度

java - JComponent 上的故障绘图

java - (卡布局)设置JComboBox的CardPanels卡名

java - glassfish smtp 来自名称

java - 现有对象的编辑应该在存储库层还是在服务中完成?

java - flex 应用程序在使用 XmlSocket 时重置连接

java - Spring Boot 忽略 application.yml 中的数据源 url

java - JScrollPane 在 GUI 中不起作用

java - 需要帮助为用户功能添加难度选项

java - jTable中的jButton不可点击