Hi everybody. First of all thanks for taking the time to read this. I'm having a problem with initializing the buttons and adding them to JPanel. The problem is with everything else I have I'm trying to add the buttons in initButtons(); to the JPanel but I can't seem to figure out how to add it, because I want the buttons to appear anytime they want to play and I want for when they press one of the alphabet buttons(I'm doing hangman) that it goes through the buttonClicked method. Basically I don't know what to do to call on those methods. I'm sorry if this is a really simple question that has a fairly simple answer, I am 3 months into this. THANKS A LOT.Here it is:

//starting to create the game window/frame
 JPanel gameFramePanel= new JPanel(new FlowLayout());
 //adding Action Listener to buttons for the game window
 
 gameFrameInstructionsButton.addActionListener(this);
 gameFrameMainButton.addActionListener(this);
 gameFrameExitButton.addActionListener(this);
 guessWordButton.addActionListener(this);
 
 
 //add buttons to FramePanel
 
 gameFramePanel.add(gameFrameInstructionsButton);
 gameFramePanel.add(gameFrameMainButton);
 gameFramePanel.add(gameFrameExitButton);
 gameFramePanel.add(guessWordButton);
  
 //Organize content pane
 
 JPanel pane=new JPanel (new BorderLayout());
  
 //Add Labelled input fields to display
 JPanel inFieldPane=new JPanel(new GridLayout(3,11));
  
 //adding the labels
 JLabel guessILabel = new JLabel();
 inFieldPane.add(new JLabel("Guess"));
inFieldPane.add(guessI);
 pane.add(inFieldPane,BorderLayout.CENTER);
   
 //Add Output fields
 
 JPanel outFieldPane=new JPanel(new GridLayout(1,2));//creating and outFieldPane
   
 outFieldPane.add(answerReal);
 outFieldPane.add(yourGuess);
 outFieldPane.add(incorrectGuess);
 pane.add(gameFramePanel, BorderLayout.NORTH);
 pane.add(outFieldPane,BorderLayout.SOUTH);
 gameFrame.setContentPane(pane);
  
 // set Content Pane and Size
 gameFrame.setVisible(false);
 gameFrame.pack();
 gameFrame.setSize(800,300);        
 
 }

private void initButtons()
{
 button=new JButton[26];
 for(int index=0; index<26;index++)
 {
 	
 	button[index]=new JButton(""+index);
 	String letter = Character.toString((char)(index + 65));
 	button[index] = new JButton(letter);
	 button[index].addActionListener(this);
 	button[index].addMouseListener(new MyMouseAdapter(letter));
 	gameFramePanel.add(button[index]);
 }

}
private void buttonClicked(String letter) {
        if(alphabet.length==pilarF.length)
        {
        	System.out.println("gdfdd");
        }
                
          }
    private class MyMouseAdapter extends MouseAdapter {
        private String letter;
        public MyMouseAdapter(String letter) {
            this.letter = letter;
        }
        public void mouseClicked(MouseEvent evt) {
            buttonClicked(this.letter);
        }
    }

Instead of extending MouseAdapter, you could just have that class implement ActionListener and put the code in mouseClicked() into the actionPerformed() method. Then use a new instance of the class instead of "this" for the argument to button[index].addActionListener().

Thanks. I have taken that into consideration. And thanks for taking the time to reply to this simple question. have a fantastic day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.