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