Java中非常简单的Hangman程序

时间:2016-12-04 14:36:33

标签: java swing user-interface netbeans swingx

我想制作一个非常简单的hangman程序,其GUI使用6个图像来显示游戏的进展。我想硬编码“Hello”这个词,并且每个错误都会让用户改变图像。 (共6张渐进式图像)。

我已经设法在Netbeans上使用Swing库制作GUI,但我对如何继续实际代码感到难过。 有什么指针吗?

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Random;
import java.io.*;


 /*
  * To change this license header, choose License Headers in Project               
Properties.

 * To change this template file, choose Tools | Templates
  * and open the template in the editor.
  */

 /**
  *
  * @author ruzni
  */
 public class GuessTheWord extends javax.swing.JPanel {

     /**
      * Creates new form GuessTheWord
      */
     public GuessTheWord() {
        initComponents();
    }

     /**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated    Code">                          
      private void initComponents() {

    char0 = new javax.swing.JLabel();
    char1 = new javax.swing.JLabel();
    char2 = new javax.swing.JLabel();
    char3 = new javax.swing.JLabel();
    char4 = new javax.swing.JLabel();
    char5 = new javax.swing.JLabel();
    aLetter = new javax.swing.JTextField();
    TryLetter = new javax.swing.JButton();
    Img = new javax.swing.JButton();

    char0.setText("X");

    char1.setText("X");

    char2.setText("X");

    char3.setText("X");

    char4.setText("X");

    char5.setText("X");

    aLetter.setText("e");
    aLetter.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            aLetterActionPerformed(evt);
        }
    });

    TryLetter.setText("TryLetter");
    TryLetter.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            TryLetterMouseClicked(evt);
        }
    });

    Img.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img000.png"))); // NOI18N
    Img.setText("jButton3");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(20, 20, 20)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(TryLetter)
                .addComponent(aLetter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(char0)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char5)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
            .addComponent(Img, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(21, 21, 21))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(aLetter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(26, 26, 26)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(char0)
                        .addComponent(char1)
                        .addComponent(char2)
                        .addComponent(char3)
                        .addComponent(char4)
                        .addComponent(char5))
                    .addGap(71, 71, 71)
                    .addComponent(TryLetter))
                .addGroup(layout.createSequentialGroup()
                    .addGap(29, 29, 29)
                    .addComponent(Img, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(36, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void aLetterActionPerformed(java.awt.event.ActionEvent evt) {                                        
//GEN-FIRST:event_guessActionPerformed
}                                       

private void TryLetterMouseClicked(java.awt.event.MouseEvent evt) {                                      

}                                     



// Variables declaration - do not modify                     
private javax.swing.JButton Img;
private javax.swing.JButton TryLetter;
private javax.swing.JTextField aLetter;
private javax.swing.JLabel char0;
private javax.swing.JLabel char1;
private javax.swing.JLabel char2;
private javax.swing.JLabel char3;
private javax.swing.JLabel char4;
private javax.swing.JLabel char5;
// End of variables declaration                   

}

1 个答案:

答案 0 :(得分:0)

首先,您需要从背景代码开始,而不是可见部分。

例如,您可以创建一个名为Hangman的类,它为您提供了一种更改进度的方法(添加错误),以及一个为您提供当前绘制状态图像的方法。

伪代码如:

public class Hangman
{
    private int mistakesDone;

    private HashMap<Integer, Image> progressImages;

    public Hangman()
    {
        mistakesDone = 0;
        progressImages = new HashMap<Integer, Image>();

        for(int i = 0; i < 7;i++)
            progressImages.put(i, new ImageIcon(new URL("hangman_" + i)).getImage());
    }

    public Image getCurrentImage()
    {
        return proressImages.get(mistakesDone);
    }

    public void mistakeDone()
    {
        mistakesDone++;
    }

    public int getMistakesDone()
    {
        return mistakesDone;
    }

    public void resetMistakes()
    {
        mistakesDone = 0;
    }
}

现在您已经获得了Hangman类,您需要在Main-Game-Class中创建一个Object对象,或者您可以调用它。想要绘制时,可以让对象获取当前图像。记得更新错误完成!

我建议你也要对猜测部分的基本代码做同样的事情! (Outsourcsing在面向对象语言中是一件非常好的事情)

祝你好运,如果事情不清楚,请评论这篇文章。