数字拼图编码

时间:2017-06-10 06:17:28

标签: 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);
    }
}                                                                        

看起来很长很慢。我怎样才能缩短它?

1 个答案:

答案 0 :(得分:2)

试试这个。

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...