Results 1 to 2 of 2
  1. #1
    br26354 is offline Member
    Join Date
    Apr 2015
    Posts
    1
    Rep Power
    0

    Default Trying to create a Hangman game with GUI

    I am trying to create a Hangman game with a gui. i already have buttons for the letters, each with its own action listener. I'm having difficulties creating the gallows and man.

    I have a class called guiTest, which contains all of the letters. This is essentially what i have. Can I add the image to this class, or do I need to create a new class that extends JFrame?

    Java Code:
     import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Container;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    
    import java.awt.Graphics;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.Border;
    import java.util.*;
    
    public class GUITest extends JFrame{
    	private final JButton aButton ;
    	private final JButton bButton ;
    	private final JButton cButton ;
    	private final JButton dButton ;
    	private final JButton eButton ;
    	private final JButton fButton ;
    	private final JButton gButton ;
    	private final JButton hButton ;
    	private final JButton iButton ;
    	private final JButton jButton ;
    	private final JButton kButton ;
    	private final JButton lButton ;
    	private final JButton mButton ;
    	private final JButton nButton ;
    	private final JButton oButton ;
    	private final JButton pButton ;
    	private final JButton qButton ;
    	private final JButton rButton ;
    	private final JButton sButton ;
    	private final JButton tButton ;
    	private final JButton uButton ;
    	private final JButton vButton ;
    	private final JButton wButton ;
    	private final JButton xButton ;
    	private final JButton yButton ;
    	private final JButton zButton ;
    	private final JButton newWButton ;
    private final JButton showWButton ;
    	private final JButton quitButton ;
    private final JButton startButton ;
    	
    	private final FlowLayout layout;
    	private final Container container;
    	
    	public static void main (String[] args){
    		GUITest gui = new GUITest();
    		Man man = new Man();
    		gui.setSize(800,900);
    		gui.setVisible(true);
    		
    	}
      
    	 
    		 public GUITest()
         {
        	 
        	 
    
    		
    		
    		
    		layout = new FlowLayout();
        	 container = getContentPane();
        	 setLayout(null);
        	 
             aButton = new JButton("A");
        	
             add(aButton);
        	 aButton.setBounds(121, 520, 50, 29);
        	aButton.addActionListener(
        			 new ActionListener()
        			 {
        			 @Override
        			    public void actionPerformed(ActionEvent event)
        			    {
        				 
        			    }
        			 }
        			 );
        		
        	
        	
        	   bButton = new JButton("B");
          	 add(bButton);
          	bButton.setBounds(170, 520, 50, 29);
          	bButton.addActionListener(
          			 new ActionListener()
          			 {
          			 @Override
          			    public void actionPerformed(ActionEvent event)
          			    {
          				 
          			    }
          			 }
          			 );
            cButton = new JButton("C");
       	 add(cButton);
       	cButton.setBounds(219, 520, 50, 29);
       	cButton.addActionListener(
       			 new ActionListener()
       			 {
       			 @Override
       			    public void actionPerformed(ActionEvent event)
       			    {
       				 
       			    }
       			 }
       			 );
        dButton = new JButton("D");
    	 add(dButton);
    	 dButton.setBounds(268, 520, 50, 29);
    	dButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   eButton = new JButton("E");
      	 add(eButton);
      	eButton.setBounds(317, 520, 50, 29);
      	eButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        fButton = new JButton("F");
    	 add(fButton);
    	 fButton.setBounds(366, 520, 50, 29);
    	fButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   gButton = new JButton("G");
      	 add(gButton);
      	gButton.setBounds(415, 520, 50, 29);
      	gButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        hButton = new JButton("H");
    	 add(hButton);
    	 hButton.setBounds(464, 520, 50, 29);
    	hButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   iButton = new JButton("I");
      	 add(iButton);
      	iButton.setBounds(513, 520, 50, 29);
      	iButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        jButton = new JButton("J");
    	 add(jButton);
    	 jButton.setBounds(562, 520, 50, 29);
    	jButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   kButton = new JButton("K");
      	 add(kButton);
      	kButton.setBounds(611, 520, 50, 29);
      	kButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        lButton = new JButton("L");
    	 add(lButton);
    	 lButton.setBounds(143, 550, 50, 29);
    	lButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   mButton = new JButton("M");
      	 add(mButton);
      	mButton.setBounds(192, 550, 50, 29);
      	mButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        nButton = new JButton("N");
    	 add(nButton);
    	 nButton.setBounds(241, 550, 50, 29);
    	nButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   oButton = new JButton("O");
      	 add(oButton);
      	oButton.setBounds(290, 550, 50, 29);
      	oButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        pButton = new JButton("P");
    	 add(pButton);
    	 pButton.setBounds(339, 550, 50, 29);
    	pButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   qButton = new JButton("Q");
      	 add(qButton);
      	qButton.setBounds(388, 550, 50, 29);
      	qButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        rButton = new JButton("R");
    	 add(rButton);
    	 rButton.setBounds(437, 550, 50, 29);
    	rButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   sButton = new JButton("S");
      	 add(sButton);
      	sButton.setBounds(486, 550, 50, 29);
      	sButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        tButton = new JButton("T");
    	 add(tButton);
    	 tButton.setBounds(535, 550, 50, 29);
    	tButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   uButton = new JButton("U");
      	 add(uButton);
      	uButton.setBounds(584, 550, 50, 29);
      	uButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        vButton = new JButton("V");
    	 add(vButton);
    	 vButton.setBounds(270, 580, 50, 29);
    	vButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   wButton = new JButton("W");
      	 add(wButton);
      	 wButton.setBounds(319, 580, 50, 29);
      	wButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        xButton = new JButton("X");
    	 add(xButton);
    	 xButton.setBounds(368, 580, 50, 29);
    	xButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	   yButton = new JButton("Y");
      	 add(yButton);
      	 yButton.setBounds(417, 580, 50, 29);
      	yButton.addActionListener(
      			 new ActionListener()
      			 {
      			 @Override
      			    public void actionPerformed(ActionEvent event)
      			    {
      				 
      			    }
      			 }
      			 );
        zButton = new JButton("Z");
    	 add(zButton);
    	 zButton.setBounds(466, 580, 50, 29);
    	zButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	
    	 startButton = new JButton("Start Game");
    	 add(startButton);
    	 startButton.setBounds(100, 700, 120, 29);
    	startButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    
    	 newWButton = new JButton("New Word");
    	 add(newWButton);
    	 newWButton.setBounds(250, 700, 120, 29);
    	newWButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    	 showWButton = new JButton("Show Word");
    	 add(showWButton);
    	 showWButton.setBounds(400, 700, 120, 29);
    	showWButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
    
    	 quitButton = new JButton("Quit Game");
    	 add(quitButton);
    	 quitButton.setBounds(550, 700, 120, 29);
    	quitButton.addActionListener(
    			 new ActionListener()
    			 {
    			 @Override
    			    public void actionPerformed(ActionEvent event)
    			    {
    				 
    			    }
    			 }
    			 );
        
    
         }
    
    	
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to create a Hangman game with GUI

    Yes, you can include the drawing in the same frame. It would be best to put it inside a JPanel in the frame.

    Also, a couple observations.

    1. If want to use buttons, don't use absolute positioning. Use a layout manager and simply add them to a JPanel. FlowLayout would be good for this but you could do some testing.
    2. You only need one ActionListener. You can determine which button was clicked by using the getSource() from the action event.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Hangman Game
    By getshum in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 04:01 AM
  2. Hangman Game // HELP //
    By K-Scale in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 12:01 AM
  3. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 02:24 PM
  4. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 06:47 PM
  5. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:16 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
  •