Hey guys i m trying to make hangman game as java application

here is a simple run how it should be.
http://e.atilim.edu.tr/mod/resource/view.php?id=5240

i m trying to put JLabels in order when player click correct buttons.

firstly i added JFrames at first then i did setEnabled(false) them

after button is clicked it i did setEnabled(true)
problem is that it dont show the JLabel after button's clicked
here is the code;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.sun.org.apache.bcel.internal.generic.PUTFIELD;




public class Hangman {
	boolean found = false;
	JFrame frm = new JFrame("Game Hangman");
	private JPanel pnl0= new JPanel();
	private JPanel pnl1= new JPanel();
	private JPanel pnl2= new JPanel();
	private JPanel pnl3= new JPanel();
	private JButton Aa = new JButton("A");
	private JButton B = new JButton("B");
	private JButton C = new JButton("C");
	private JButton cc = new JButton("c");
	private JButton D = new JButton("D");
	private JButton E = new JButton("E");
	private JButton F = new JButton("F");
	private JButton G = new JButton("G");
	private JButton gg = new JButton("g");
	private JButton H = new JButton("H");
	private JButton I = new JButton("I");
	private JButton ii = new JButton("i");
	private JButton J = new JButton("J");
	private JButton K = new JButton("K");
	private JButton L = new JButton("L");
	private JButton M= new JButton("M");
	private JButton N = new JButton("N");
	private JButton O = new JButton("O");
	private JButton oo = new JButton("Ö");
	private JButton P = new JButton("P");
	private JButton R = new JButton("R");
	private JButton S = new JButton("S");
	private JButton ss = new JButton("s");
	private JButton T = new JButton("T");
	private JButton U = new JButton("U");
	private JButton uu = new JButton("Ü");
	private JButton V = new JButton("V");
	private JButton Y = new JButton("Y");
	private JButton Z = new JButton("Z");
	
	private JLabel imgLbc  = new JLabel();
	
	private ImageIcon a[]=new ImageIcon[100];
	private JLabel b[]=new JLabel[100];
	private  JLabel k[]=new JLabel[100];
	private JLabel imgLbb = new JLabel();
	private ImageIcon hang = new ImageIcon("Hangman.png");
	private String txt;
	private int[] kk;
	private int count=0;
	
	
public Hangman(){
	
	System.out.println("Enter your keyword to be find by person ");
	Scanner scan = new Scanner(System.in);
	txt=scan.next();
	
for(int zt=0;zt<txt.length();zt++){
		
		
		k[zt]=new JLabel(txt.charAt(zt)+"");
		
		
		pnl1.add(k[zt]);
		
if(!found){		
		
		k[zt].setVisible(false);
}
	}

for(int i=0;i<txt.length();i++){
		
		//a[i]= new ImageIcon("Çizgi.gif");
		b[i]= new JLabel("______");
		//b[i].setIcon(a[i]);
		
		pnl1.add(b[i]);
		
	}
	
	
	Aa.addActionListener(new ActionListener(){
		
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='a'||txt.charAt(i)=='A' ){
				Aa.setBackground(Color.GREEN);
				Aa.setEnabled(false);
				
				break;
			}
			
			else
				Aa.setBackground(Color.RED);
		
		
			
			
			}
			for(int tt=0;tt<txt.length();tt++){
				
				
				if(txt.charAt(tt)=='a'||txt.charAt(tt)=='A'){
					
					 
				k[tt].setEnabled(true);
				pnl1.add(k[tt]);
				found=true;
				}
			
				
			}
			
			
			
		}
			
			
			
	});

	
	
	
	B.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='b'||txt.charAt(i)=='B' ){
				B.setBackground(Color.GREEN);
				B.setEnabled(false);
				break;
			}
			else
				B.setBackground(Color.RED);
		}
			
			for(int tt=0;tt<txt.length();tt++){
				
				if(txt.charAt(tt)=='b'||txt.charAt(tt)=='B'){
				k[tt]= new JLabel("B");
				k[tt].setEnabled(true);
			}
				
			}
			
			
			
		
		}
	});
	C.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='c'||txt.charAt(i)=='C' ){
				C.setBackground(Color.GREEN);
				break;
			}
			else
				C.setBackground(Color.RED);
			}
			
			
		
		}
	});
	D.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='d'||txt.charAt(i)=='D' ){
				D.setBackground(Color.GREEN);
				break;
			}
			else
				D.setBackground(Color.RED);
			}
			
			
		
		}
	});
	E.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='e'||txt.charAt(i)=='E' ){
				E.setBackground(Color.GREEN);
				break;
			}
			else
				E.setBackground(Color.RED);
			}
			
			
		
		}
	});
	F.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='f'||txt.charAt(i)=='F' ){
				F.setBackground(Color.GREEN);
				break;
			}
			else
				F.setBackground(Color.RED);
		
			}
			
			
		}
	});
	G.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='g'||txt.charAt(i)=='G' ){
				G.setBackground(Color.GREEN);
				break;
			}
			else
				G.setBackground(Color.RED);
			}
			
			
		
		}
	});					
	gg.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='a'||txt.charAt(i)=='A' ){
				Aa.setBackground(Color.GREEN);
				
			}
			else
				Aa.setBackground(Color.RED);
			}
			
			
		
		}
	});
	H.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='h'||txt.charAt(i)=='H' ){
				H.setBackground(Color.GREEN);
				break;
			}
			else
				H.setBackground(Color.RED);
			}
			
			
		
		}
	});
	ii.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='a'||txt.charAt(i)=='A' ){
				ii.setBackground(Color.GREEN);
				break;
			}
			else
				ii.setBackground(Color.RED);
			}
			
			
		
		}
	});
	I.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='a'||txt.charAt(i)=='A' ){
				I.setBackground(Color.GREEN);
				break;
			}
			else
				I.setBackground(Color.RED);
			}
			for(int tt=0;tt<txt.length();tt++){
				
			if(txt.charAt(tt)=='i'||txt.charAt(tt)=='I'){
				k[tt]= new JLabel("I");
				
				
				
			}
				
			}
			
		
		}
	});
	J.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='j'||txt.charAt(i)=='J' ){
				Aa.setBackground(Color.GREEN);
				
			}
			else
				J.setBackground(Color.RED);
			}
			
			
		
		}
	});
	K.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='k'||txt.charAt(i)=='K' ){
				K.setBackground(Color.GREEN);
				break;
			}
			else
				K.setBackground(Color.RED);
		
			}
			
			
		}
	});
	L.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='l'||txt.charAt(i)=='L' ){
				L.setBackground(Color.GREEN);
				break;
			}
			else
				L.setBackground(Color.RED);
			}
			
			
		
		}
	});
	M.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='m'||txt.charAt(i)=='M' ){
				M.setBackground(Color.GREEN);
				break;
			}
			else
				M.setBackground(Color.RED);
			}
			
			
		
		}
	});
	N.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='n'||txt.charAt(i)=='N' ){
				N.setBackground(Color.GREEN);
				break;
			}
			else
				N.setBackground(Color.RED);
			}
			
			
		
		}
	});
	O.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='o'||txt.charAt(i)=='O' ){
				O.setBackground(Color.GREEN);
				break;
			}
			else
				O.setBackground(Color.RED);
			}
			
			
		
		}
	});
	oo.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='a'||txt.charAt(i)=='A' ){
				oo.setBackground(Color.GREEN);
				break;
			}
			else
				oo.setBackground(Color.RED);
		
			}
			
			
		}
	});
	
	P.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='p'||txt.charAt(i)=='P' ){
				P.setBackground(Color.GREEN);
				break;
			}
			else
				P.setBackground(Color.RED);
		
			}
			
			
		
		}
	});
	R.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='r'||txt.charAt(i)=='R' ){
				R.setBackground(Color.GREEN);
				break;
			}
			else
				R.setBackground(Color.RED);
			}
			
			
		
		}
	});
	S.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='s'||txt.charAt(i)=='S' ){
				Aa.setBackground(Color.GREEN);
				break;
			}
			else
				S.setBackground(Color.RED);
		
			}
			
			
		}
	});
	ss.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='o'||txt.charAt(i)=='O' ){
				ss.setBackground(Color.GREEN);
				break;
			}
			else
				ss.setBackground(Color.RED);
			}
			
			
		
		}
	});
	T.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='t'||txt.charAt(i)=='T' ){
				T.setBackground(Color.GREEN);
				break;
			}
			else
				T.setBackground(Color.RED);
			}
			
			
		
		}
	});
	U.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='u'||txt.charAt(i)=='U' ){
				U.setBackground(Color.GREEN);
				break;
			}
			else
				U.setBackground(Color.RED);
			}
			
			
		
		}
	});
	
	uu.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='u'||txt.charAt(i)=='U' ){
				uu.setBackground(Color.GREEN);
				break;
			}
			else
				uu.setBackground(Color.RED);
			}
			
			
		
		}
	});
	V.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='v'||txt.charAt(i)=='V' ){
				V.setBackground(Color.GREEN);
				break;
			}
			else
				V.setBackground(Color.RED);
			}
			
			
		
		}
	});
	Y.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='y'||txt.charAt(i)=='Y' ){
				Y.setBackground(Color.GREEN);
				break;
			}
			else
				Y.setBackground(Color.RED);
		
			}
			
			
		}
	});
	Z.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0){
			
			for(int i=0;i<txt.length();i++){
			if(txt.charAt(i)=='z'||txt.charAt(i)=='Z' ){
				Z.setBackground(Color.GREEN);
				break;
			}
			else
				Z.setBackground(Color.RED);
		
			}
			
			
		}
	});

	
	
	
	
	
	
	
	
	
	
	
	
	pnl3.setLayout(new BorderLayout());
	imgLbb.setIcon(hang);
	pnl3.add(imgLbb);
	

	pnl0.setLayout(new GridLayout(0,txt.length()));
	pnl1.setLayout(new GridLayout(0,txt.length()));
	pnl2.setLayout(new GridLayout(0,8));
	pnl2.add(Aa);
	pnl2.add(B);
	pnl2.add(C);
	pnl2.add(cc);
	pnl2.add(D);
	pnl2.add(E);
	pnl2.add(F);
	pnl2.add(G);
	pnl2.add(gg);
	pnl2.add(H);
	pnl2.add(I);
	pnl2.add(ii);
	pnl2.add(J);
	pnl2.add(K);
	pnl2.add(L);
	pnl2.add(M);
	pnl2.add(N);
	pnl2.add(O);
	pnl2.add(oo);
	pnl2.add(P);
	pnl2.add(R);
	pnl2.add(S);
	pnl2.add(ss);
	pnl2.add(T);
	pnl2.add(U);
	pnl2.add(uu);
	pnl2.add(V);
	pnl2.add(Y);
	pnl2.add(Z);

	
	frm.setLayout(new BorderLayout());
	frm.add(pnl0,BorderLayout.NORTH);
	frm.add(pnl1,BorderLayout.CENTER);
	frm.add(pnl3,BorderLayout.EAST);
	frm.add(pnl2,BorderLayout.SOUTH);
	
	frm.pack();
	
	frm.setVisible(true);
	frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frm.setPreferredSize(new Dimension(1000,1000));
	
}


public static void main(String[] args) {
	Hangman app = new Hangman();
	
}
}

Firstly, When I click on the link it requires a login to continue

Also it would be better to put some parts of the code in a new class to make it efficient and lessen the lines of code plus it can help us pinpoint what part of the code has the error Cause its really hard to find an error with a program that has 675 lines of code.

Lastly some comments on each method would be nice

You can also use this to make it more presentable and efficient

import javax.swing.*;
      import java.awt.*;

im on proggress. I succed at putting JLabel in order.

but now i have problem about destroying them.

After text found or failing after 6 atempt i put button Continue.

after this button pressed there should be new JLabel. I succed at it. But promlem is after i use setEnable(false) command ,previous jlabels still are on gui .


how to i delete them ?

dont tell me use setVisible(false) cuz these commond dont delete them on panel. i have to delete perivous jlabel.

thank you

Here is a link.What im trying to do
http://www.dosya.tc/server6/x5MgCs/assignment1_1_.doc.html

Edited 5 Years Ago by KSBeyaz: n/a

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