2016-12-04 64 views
-2

我想用一個GUI來製作一個非常簡單的hang子手程序,該程序使用6個圖像來顯示遊戲的進程。我想對單詞「Hello」進行硬編碼,並且每次用戶都會使圖像發生變化。 (總共6張連續圖像)。Java中的非常簡單的Hang子手程序

我已經設法使用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     

}

+0

下面是一個指針:完全相反。忘記用戶界面,直到你有一個對象模型,當你在命令行上使用文本驅動它時,其行爲與你想要的完全一致。一旦你有了,你可以安全地繼續進行Swing。 – duffymo

回答

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; 
    } 
} 

現在,你有你的劊子手類,你需要在你的主遊戲-Class,以創建它的對象或者無論你叫它。當你想繪畫的時候,你可以讓Object來獲取當前的圖像。記住更新錯誤完成!

我會推薦你​​做同樣的猜測部分的基本代碼! (Outsourcsing在面向對象的語言中是一件非常好的事情)

祝你好運,如果有些事不明確,請發表評論。

+0

謝謝!我設法解決它:) –