Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By KSBeyaz

Thread: Hangman with GUİ

  1. #1
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Hangman with GUİ

    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 JLabe after button's clicked
    here is the code;
    Java 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();
    	
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Hangman with GUİ

    Before going any further, let's first try to refactor your code. You do understand that by using an array of Strings and a for loop, over 80% of your code can be removed. Which do you think is easier to debug and improve on, your code above, or a simpler but similar program like mine below?

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class BunchOfButtons extends JPanel {
       public static final String[] BUTTON_TEXTS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", "."};
       
       public BunchOfButtons() {
          setLayout(new GridLayout(0, 3));
    
          // create one ActionListener for all buttons
          ButtonListener btnListener = new ButtonListener();
    
          for (String btnText : BUTTON_TEXTS) {
             JButton button = new JButton(btnText);
    
             // and add that single ActionListener to the buttons in a for loop
             // as I create the buttons
             button.addActionListener(btnListener);
    
             add(button);
          }
       }
       
       private class ButtonListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
    
             // get the button's text
             String actionCommand = e.getActionCommand();
             
             // then do something with that text
             System.out.println("Button Pressed: " + actionCommand);
             // your program will do a lot more with the text of course
          }
       }
       
       private static void createAndShowGui() {
          BunchOfButtons mainPanel = new BunchOfButtons();
    
          JFrame frame = new JFrame("Bunch Of Buttons");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

  3. #3
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: Hangman with GUİ

    thank you for your reply and example.

    i will try to convert my codes into your style which you include.

    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/GYnoDv/a...t1_1_.doc.html
    Last edited by KSBeyaz; 11-19-2011 at 07:42 PM.

  4. #4
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: Hangman with GUİ

    any help ??

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Hangman with GUİ

    Have you looked at the API doc for containers?
    Your code uses the add method. Is there a corresponding method that will delete/remove a component from a container?

  6. #6
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: Hangman with GUİ

    here is on hangman class calling method from another class

    Java Code:
    for(int tt=0;tt<txt.length();tt++){
    				
    			panels[tt].setDisable();
    here is method(In this class used add(charLbl) and add(lineLbl)but they are not added into any panel)

    Java Code:
    public void setDisable(){
    		
    		remove(charLbl);
    		remove(lineLbl);
    		revalidate();
    		
    		
    		this.repaint();
    here is the charlbl and lineLbl added into pnl1
    Java Code:
    for(int zt=0;zt<txt.length();zt++){  //adding new text
    					panels[zt]=new MyPanel(txt.charAt(zt));calling another Class's constructer
    					pnl1.add(panels[zt]);//charlbl and lineLbl added into pnl1
    					panels[zt].setVisible(false);//this code for invisible charLbl cuz player should find this text .for each letter he find i did visible them

    i dont want to delete pnl1. i just want to delete charlbl and lineLbl for the next charLbl and lineLbl


    I want to delete previous JLbal cuz next jLabal isnt all of them on the line.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Hangman with GUİ

    Can you make a small simple program that compiles and executes to demonstrate your problem?

  8. #8
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: Hangman with GUİ

    It's better to put video in order to show problem

    here it is
    Hangman.mp4 - YouTube
    (sorry for my english)

  9. #9
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: Hangman with GUİ

    i solved my problem with this code

    pnl1.remove(panels[tt]);
    pnl1.revalidate();
    pnl1.repaint();
    now my Homework work perfectly :D
    Fubarable likes this.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

Similar Threads

  1. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 06:54 PM
  2. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 09:56 AM
  3. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  4. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •