hi guys, i am using eclispe 3.5 to develop a hangman game but im in no man's land now. I have created 26 alphabets buttons A-Z and i want to display the alphabets in the JTextField when the button is clicked. When the user guesses the word incorrectly the word would be displayed in another TextField which shows the used alphabets. Upon gueesing a wrong word, it would display the hang pictures i have stored in my picture folder.
This is my code so far.

public class hangman extends JPanel {


private static final long serialVersionUID = 1L;
private JLabel head = null;
private JLabel body = null;
private JLabel rightArm = null;
private JLabel leftArm = null;
private JLabel rightLeg = null;
private JLabel leftLeg = null;
private JLabel stick = null;
private JLabel hang = null;
private JLabel stand = null;
private JButton buttonA = null;
private JButton buttonB = null;
private JButton buttonC = null;
private JButton buttonD = null;
private JButton buttonE = null;
private JButton buttonF = null;
private JButton buttonG = null;
private JButton buttonH = null;
private JButton buttonI = null;
private JButton buttonJ = null;
private JButton buttonK = null;
private JButton buttonL = null;
private JButton buttonM = null;
private JButton buttonN = null;
private JButton buttonO = null;
private JButton buttonP = null;
private JButton buttonQ = null;
private JButton buttonR = null;
private JButton buttonS = null;
private JButton buttonT = null;
private JButton buttonU = null;
private JButton buttonV = null;
private JButton buttonW = null;
private JButton buttonX = null;
private JButton buttonY = null;
private JButton buttonZ = null;
private JTextField jTextField = null;
private JButton jButton = null;
final int maxWordlen = 20;
final int maxTries = 7;
char correctWord[];
int correctWordLen;
char wrongLetters [];
char words[];
int wordlen;
int wrongLettersCount;
JTextField wordFont;
static final int HANGCLASS = 1;
MediaTracker tracker;



public hangman() {
super();
initialize();
wrongLettersCount = 0;
wrongLetters = new char[maxTries];


correctWordLen = 0;
correctWord = new char[maxWordlen];


words = new char[maxWordlen];
wordFont = new javax.swing.JTextField();



}
String wordlist[] = {
"hi",
"car",
"apple",
"inception",
"jokes",
"circumstance",
"combination",
"consequently",
};
public void newGame() {
int i;


String s = wordlist[(int)Math.floor(Math.random() * wordlist.length)];


correctWordLen = Math.min(s.length(), maxWordlen);
for (i=0; i<correctWordLen; i++) {
correctWord = s.charAt(i);
}



for (i=0; i<maxWordlen; i++) {
words = 0;
}
wordlen = 0;
for (i=0; i<maxTries; i++) {
wrongLetters = 0;
}
wrongLettersCount = 0;



}


public void start() {
requestFocus();
try {
tracker.waitForID(HANGCLASS);
} catch (InterruptedException e) {}
tracker.checkAll(true);
if (correctWordLen == wordlen || wrongLettersCount == maxTries) {
newGame();
}
}



/**
* This method initializes this
*
* @return void
*/
private void initialize() {
stand = new JLabel();
stand.setBounds(new Rectangle(545, 123, 184, 442));
stand.setIcon(new ImageIcon(getClass().getResource("/picture/ui/2.jpg")));
stand.setText("");
hang = new JLabel();
hang.setBounds(new Rectangle(320, 179, 34, 59));
hang.setIcon(new ImageIcon(getClass().getResource("/picture/ui/5.jpg")));
hang.setText("");
stick = new JLabel();
stick.setBounds(new Rectangle(319, 139, 331, 54));
stick.setIcon(new ImageIcon(getClass().getResource("/picture/ui/4.jpg")));
stick.setText("");
leftLeg = new JLabel();
leftLeg.setBounds(new Rectangle(230, 494, 99, 138));
leftLeg.setIcon(new ImageIcon(getClass().getResource("/picture/ui/leftleg.jpg")));
leftLeg.setText("JLabel");
rightLeg = new JLabel();
rightLeg.setBounds(new Rectangle(330, 517, 122, 102));
rightLeg.setIcon(new ImageIcon(getClass().getResource("/picture/ui/rightleg.jpg")));
rightLeg.setText("JLabel");
leftArm = new JLabel();
leftArm.setIcon(new ImageIcon(getClass().getResource("/picture/ui/leftarm.jpg")));
leftArm.setLocation(new Point(203, 400));
leftArm.setSize(new Dimension(120, 16));
leftArm.setText("");
rightArm = new JLabel();
rightArm.setBounds(new Rectangle(337, 400, 120, 16));
rightArm.setIcon(new ImageIcon(getClass().getResource("/picture/ui/rightarm.jpg")));
rightArm.setText("");
body = new JLabel();
body.setBounds(new Rectangle(326, 384, 38, 153));
body.setIcon(new ImageIcon(getClass().getResource("/picture/ui/body.jpg")));
body.setText("");
head = new JLabel();
head.setBounds(new Rectangle(256, 238, 152, 155));
head.setIcon(new ImageIcon(getClass().getResource("/picture/ui/head.jpg")));
head.setText("");
this.setSize(1360, 766);
this.setLayout(null);
this.setBackground(Color.black);
this.add(head, null);
this.add(body, null);
this.add(rightArm, null);
this.add(leftArm, null);
this.add(rightLeg, null);
this.add(leftLeg, null);
this.add(stick, null);
this.add(hang, null);
this.add(stand, null);
this.add(getButtonA(), null);
this.add(getButtonB(), null);
this.add(getButtonC(), null);
this.add(getButtonD(), null);
this.add(getButtonE(), null);
this.add(getButtonF(), null);
this.add(getButtonG(), null);
this.add(getButtonH(), null);
this.add(getButtonI(), null);
this.add(getButtonJ(), null);
this.add(getButtonK(), null);
this.add(getButtonL(), null);
this.add(getButtonM(), null);
this.add(getButtonN(), null);
this.add(getButtonO(), null);
this.add(getButtonP(), null);
this.add(getButtonQ(), null);
this.add(getButtonR(), null);
this.add(getButtonS(), null);
this.add(getButtonT(), null);
this.add(getButtonU(), null);
this.add(getButtonV(), null);
this.add(getButtonW(), null);
this.add(getButtonX(), null);
this.add(getButtonY(), null);
this.add(getButtonZ(), null);
this.add(getJTextField(), null);
this.add(getJButton(), null);
}


/**
* This method initializes buttonA
*
* @return javax.swing.JButton
*/
private JButton getButtonA() {
if (buttonA == null) {
buttonA = new JButton();
buttonA.setBounds(new Rectangle(865, 348, 51, 24));
buttonA.setBackground(Color.orange);
buttonA.setText("A");
buttonA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonA;
}


/**
* This method initializes buttonB
*
* @return javax.swing.JButton
*/
private JButton getButtonB() {
if (buttonB == null) {
buttonB = new JButton();
buttonB.setBackground(Color.orange);
buttonB.setLocation(new Point(945, 348));
buttonB.setSize(new Dimension(51, 24));
buttonB.setText("B");
buttonB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonB;
}


/**
* This method initializes buttonC
*
* @return javax.swing.JButton
*/
private JButton getButtonC() {
if (buttonC == null) {
buttonC = new JButton();
buttonC.setBackground(Color.orange);
buttonC.setLocation(new Point(1019, 346));
buttonC.setSize(new Dimension(51, 24));
buttonC.setText("C");
buttonC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonC;
}


/**
* This method initializes buttonD
*
* @return javax.swing.JButton
*/
private JButton getButtonD() {
if (buttonD == null) {
buttonD = new JButton();
buttonD.setBackground(Color.orange);
buttonD.setLocation(new Point(1096, 344));
buttonD.setSize(new Dimension(51, 24));
buttonD.setText("D");
buttonD.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonD;
}


/**
* This method initializes buttonE
*
* @return javax.swing.JButton
*/
private JButton getButtonE() {
if (buttonE == null) {
buttonE = new JButton();
buttonE.setText("E");
buttonE.setSize(new Dimension(51, 24));
buttonE.setBackground(Color.orange);
buttonE.setLocation(new Point(1176, 346));
buttonE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonE;
}


/**
* This method initializes buttonF
*
* @return javax.swing.JButton
*/
private JButton getButtonF() {
if (buttonF == null) {
buttonF = new JButton();
buttonF.setLocation(new Point(866, 397));
buttonF.setText("F");
buttonF.setBackground(Color.orange);
buttonF.setSize(new Dimension(51, 24));
buttonF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonF;
}


/**
* This method initializes buttonG
*
* @return javax.swing.JButton
*/
private JButton getButtonG() {
if (buttonG == null) {
buttonG = new JButton();
buttonG.setLocation(new Point(941, 400));
buttonG.setText("G");
buttonG.setBackground(Color.orange);
buttonG.setSize(new Dimension(51, 24));
buttonG.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonG;
}


/**
* This method initializes buttonH
*
* @return javax.swing.JButton
*/
private JButton getButtonH() {
if (buttonH == null) {
buttonH = new JButton();
buttonH.setText("H");
buttonH.setLocation(new Point(1021, 400));
buttonH.setSize(new Dimension(51, 24));
buttonH.setBackground(Color.orange);
buttonH.setPreferredSize(new Dimension(51, 24));
buttonH.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonH;
}


/**
* This method initializes buttonI
*
* @return javax.swing.JButton
*/
private JButton getButtonI() {
if (buttonI == null) {
buttonI = new JButton();
buttonI.setText("I");
buttonI.setSize(new Dimension(51, 24));
buttonI.setBackground(Color.orange);
buttonI.setLocation(new Point(1096, 398));
buttonI.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonI;
}


/**
* This method initializes buttonJ
*
* @return javax.swing.JButton
*/
private JButton getButtonJ() {
if (buttonJ == null) {
buttonJ = new JButton();
buttonJ.setLocation(new Point(1176, 398));
buttonJ.setText("J");
buttonJ.setBackground(Color.orange);
buttonJ.setSize(new Dimension(51, 24));
buttonJ.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonJ;
}


/**
* This method initializes buttonK
*
* @return javax.swing.JButton
*/
private JButton getButtonK() {
if (buttonK == null) {
buttonK = new JButton();
buttonK.setMnemonic(KeyEvent.VK_UNDEFINED);
buttonK.setLocation(new Point(867, 443));
buttonK.setSize(new Dimension(51, 24));
buttonK.setText("K");
buttonK.setBackground(Color.orange);
buttonK.setPreferredSize(new Dimension(51, 24));
buttonK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonK;
}


/**
* This method initializes buttonL
*
* @return javax.swing.JButton
*/
private JButton getButtonL() {
if (buttonL == null) {
buttonL = new JButton();
buttonL.setLocation(new Point(942, 441));
buttonL.setText("L");
buttonL.setBackground(Color.orange);
buttonL.setSize(new Dimension(51, 24));
buttonL.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonL;
}


/**
* This method initializes buttonM
*
* @return javax.swing.JButton
*/
private JButton getButtonM() {
if (buttonM == null) {
buttonM = new JButton();
buttonM.setPreferredSize(new Dimension(51, 24));
buttonM.setSize(new Dimension(51, 24));
buttonM.setText("M");
buttonM.setBackground(Color.orange);
buttonM.setLocation(new Point(1018, 446));
buttonM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonM;
}


/**
* This method initializes buttonN
*
* @return javax.swing.JButton
*/
private JButton getButtonN() {
if (buttonN == null) {
buttonN = new JButton();
buttonN.setLocation(new Point(1101, 447));
buttonN.setText("N");
buttonN.setBackground(Color.orange);
buttonN.setSize(new Dimension(51, 24));
buttonN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonN;
}


/**
* This method initializes buttonO
*
* @return javax.swing.JButton
*/
private JButton getButtonO() {
if (buttonO == null) {
buttonO = new JButton();
buttonO.setLocation(new Point(1176, 448));
buttonO.setText("O");
buttonO.setBackground(Color.orange);
buttonO.setSize(new Dimension(51, 24));
buttonO.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonO;
}


/**
* This method initializes buttonP
*
* @return javax.swing.JButton
*/
private JButton getButtonP() {
if (buttonP == null) {
buttonP = new JButton();
buttonP.setLocation(new Point(865, 492));
buttonP.setText("P");
buttonP.setBackground(Color.orange);
buttonP.setSize(new Dimension(51, 24));
buttonP.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonP;
}


/**
* This method initializes buttonQ
*
* @return javax.swing.JButton
*/
private JButton getButtonQ() {
if (buttonQ == null) {
buttonQ = new JButton();
buttonQ.setLocation(new Point(942, 495));
buttonQ.setText("Q");
buttonQ.setBackground(Color.orange);
buttonQ.setSize(new Dimension(51, 24));
buttonQ.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonQ;
}


/**
* This method initializes buttonR
*
* @return javax.swing.JButton
*/
private JButton getButtonR() {
if (buttonR == null) {
buttonR = new JButton();
buttonR.setLocation(new Point(1020, 495));
buttonR.setText("R");
buttonR.setBackground(Color.orange);
buttonR.setSize(new Dimension(51, 24));
buttonR.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonR;
}


/**
* This method initializes buttonS
*
* @return javax.swing.JButton
*/
private JButton getButtonS() {
if (buttonS == null) {
buttonS = new JButton();
buttonS.setLocation(new Point(1100, 494));
buttonS.setText("S");
buttonS.setBackground(Color.orange);
buttonS.setSize(new Dimension(51, 24));
buttonS.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonS;
}


/**
* This method initializes buttonT
*
* @return javax.swing.JButton
*/
private JButton getButtonT() {
if (buttonT == null) {
buttonT = new JButton();
buttonT.setBackground(Color.orange);
buttonT.setSize(new Dimension(51, 24));
buttonT.setText("T");
buttonT.setLocation(new Point(1181, 496));
buttonT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonT;
}


/**
* This method initializes buttonU
*
* @return javax.swing.JButton
*/
private JButton getButtonU() {
if (buttonU == null) {
buttonU = new JButton();
buttonU.setLocation(new Point(863, 537));
buttonU.setText("U");
buttonU.setBackground(Color.orange);
buttonU.setSize(new Dimension(51, 24));
buttonU.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonU;
}


/**
* This method initializes buttonV
*
* @return javax.swing.JButton
*/
private JButton getButtonV() {
if (buttonV == null) {
buttonV = new JButton();
buttonV.setLocation(new Point(942, 538));
buttonV.setText("V");
buttonV.setBackground(Color.orange);
buttonV.setSize(new Dimension(51, 24));
buttonV.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonV;
}


/**
* This method initializes buttonW
*
* @return javax.swing.JButton
*/
private JButton getButtonW() {
if (buttonW == null) {
buttonW = new JButton();
buttonW.setBackground(Color.orange);
buttonW.setSize(new Dimension(51, 24));
buttonW.setText("W");
buttonW.setLocation(new Point(1016, 541));
buttonW.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonW;
}


/**
* This method initializes buttonX
*
* @return javax.swing.JButton
*/
private JButton getButtonX() {
if (buttonX == null) {
buttonX = new JButton();
buttonX.setLocation(new Point(1102, 538));
buttonX.setText("X");
buttonX.setBackground(Color.orange);
buttonX.setSize(new Dimension(51, 24));
buttonX.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonX;
}


/**
* This method initializes buttonY
*
* @return javax.swing.JButton
*/
private JButton getButtonY() {
if (buttonY == null) {
buttonY = new JButton();
buttonY.setBackground(Color.orange);
buttonY.setSize(new Dimension(51, 24));
buttonY.setText("Y");
buttonY.setLocation(new Point(1180, 537));
buttonY.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonY;
}


/**
* This method initializes buttonZ
*
* @return javax.swing.JButton
*/
private JButton getButtonZ() {
if (buttonZ == null) {
buttonZ = new JButton();
buttonZ.setLocation(new Point(863, 588));
buttonZ.setText("Z");
buttonZ.setBackground(Color.orange);
buttonZ.setSize(new Dimension(51, 24));
buttonZ.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return buttonZ;
}


/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(860, 117, 369, 173));
jTextField.setFont(new Font("Dialog", Font.BOLD, 14));
}
return jTextField;
}


/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(607, 667, 117, 48));
jButton.setFont(new Font("Dialog", Font.BOLD, 14));
jButton.setText("RESTART");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (correctWordLen == wordlen || wrongLettersCount == maxTries) {
newGame();
e.consume();
return;
}



}
});
}
return jButton;
}

Edited 3 Years Ago by happygeek: fixed formatting

Let's start by making your life a little easier.
There's no reason to have the same method 26 times. Can you see any way to make your getButtonA() method generic, so you can use the same method for all of the characters? I'll give you a hint: there are only two differences from one of those methods to the next, and those are variables which you have hard-coded in the methods.

See if you can do something with that, and then post your source again, this time using the code tags - paste in your source, select it all, and push the little code button on the reply box.

Also, please ask a more specific question. It's a lot easier to help you out if you tell us what you're having trouble with.

Edited 3 Years Ago by pritaeas: fixed formatting

This article has been dead for over six months. Start a new discussion instead.