Results 1 to 5 of 5
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Question generating 26 buttons

    hello

    i am tryin to generate 26 buttons but i keep getting the following error hilighted in red, i have tried shifting paragraph of codes in different places but i end up getting more errors. i would really appreciate if you could pint out where i had gone wrong.

    thank you for your time and help :)


    Java Code:
    import java.awt.Button;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class HangmanPanel extends JFrame  
    
    {
    	//this is the used letter array
    	 private boolean usd[] = new boolean[26];
    
    	 private String guessme;
    
    	 private int numguesses=0;
    
    	 private boolean finished = false;
    
    	 private boolean won = false;
    
    		private Button a[][COLOR="Red"];[/COLOR]
    	
    
    
    		public void init() {
    			
    		
    		 
    		 int i;
    		StringBuffer buffer;
    			
    
    			
    			
    			a = new Button[26];
    			
    			 new ActionListener()
    			 {
    				 public void actionPerformed (ActionEvent event)
    				 {
    			
    			
    			// create all 26 buttons
    			for (i = 0; i <26; i++) {
    			    buffer = new StringBuffer();
    				buffer.append((char)(i+65));
    				a[i] = new Button(buffer.toString());
    				a[i].addActionListener( this );
                 add(a[i]);
    			}
    			// make the guessed word uppercase
                guessme=getParameter("wrd").toUpperCase();
    					}
    				 [COLOR="Red"])[/COLOR];
    
    				
     }
    and this is my main method class

    Java Code:
     import javax.swing.JFrame;
    
    public class extend {
    
    	
    	public static void main(String args[])
    	
    	{
    		
    		
    		HangmanPanel hangmanPanel  = new HangmanPanel();
    		hangmanPanel.setVisible(true);
    		hangmanPanel.setSize(200, 200);
    		hangmanPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    }

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

    Default

    What specific error message(s) are you getting and what line throws it? Also, while this isn't the source of your error, you shouldn't be using AWT components (i.e., Button) in a Swing application. Rather, use Swing components (JButton).

    Also, if you clean up your posted code's indentation you'll have a better chance of getting help.

    Much luck!
    Last edited by Fubarable; 11-08-2009 at 01:45 PM.

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

    Default

    On looking at your code, you've got some strange things in there such as a new ActionListener call that doesn't belong where it is. I'm not really sure what you are trying to do here, and suspect that you may be better of to simply scrap this code and start over again.

    Best of luck.
    Last edited by Fubarable; 11-08-2009 at 01:55 PM.

  4. #4
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    it is highlighted in red
    ;
    and
    )

    :o

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

    Default

    Your problem is not on that line (even if it seems that it is), but rather is with the new ActionListener() call sitting out in no-where. Again, scrap this code, restart.

    I recommend that your class contain a private inner ActionListener class and that it use that. Something like:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class HangPanel extends JPanel {
      private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      private JButton[] letterButtons = new JButton[LETTERS.length()];
      
      public HangPanel() {
        setLayout(new GridLayout(2, 0, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        LetterBtnListener letterBtnListener = new LetterBtnListener();
        for (int i = 0; i < LETTERS.length(); i++) {
          String letter = String.valueOf(LETTERS.charAt(i));
          // TODO create buttons, add action listener, and add to panel
        }
      }
      
      private class LetterBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          System.out.println("Button Pressed is: " + e.getActionCommand());
          // TODO Finish this
          
        }
      }
    
    }
    Last edited by Fubarable; 11-08-2009 at 02:13 PM.

Similar Threads

  1. Excel generating from servlet
    By adeep in forum Advanced Java
    Replies: 1
    Last Post: 10-16-2009, 04:51 PM
  2. generating trigger using java
    By money3 in forum JDBC
    Replies: 1
    Last Post: 10-06-2009, 11:56 AM
  3. Generating a Keystore
    By techbossmb in forum Java Applets
    Replies: 2
    Last Post: 09-30-2009, 07:31 PM
  4. Generating PDF from XML with servlet
    By Preethi in forum Advanced Java
    Replies: 2
    Last Post: 10-30-2008, 05:50 PM
  5. Generating number sequences
    By hawkmauk in forum New To Java
    Replies: 0
    Last Post: 11-15-2007, 03:32 PM

Posting Permissions

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