1

Here is the problem, I have 26 JButton components, from which I have to select 8 at random and place them in a 2 x 2 grid in two JPanel components. When a button is clicked, I need to replace it with another random button selected from the original 26. I cannot figure out how to replace the button that is clicked with another random button. What I have so far definitely works, but may not be the best way to do it (I'm a noob obviously).

Could someone provide me with a little insight or at least lead me in the right direction?

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JVowelConsonant extends JFrame implements ActionListener
{
   int x = 0;
   String[] letter = {"a", "e", "i", "o", "u", "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
   JPanel panel1 = new JPanel(new GridLayout(2, 2));
   JPanel panel2 = new JPanel(new GridLayout(2, 2));
   JPanel panel3 = new JPanel(new FlowLayout());
   JLabel label = new JLabel("");
   JButton[] buttons = new JButton[26];

   public JVowelConsonant()
   {
      super("Vowel or Consonant?");   
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      for(x = 0; x < buttons.length; ++x)
      {
         buttons[x] = new JButton(letter[x]);
         buttons[x].addActionListener(this);
      }   
      setLayout(new FlowLayout());
      add(panel1, FlowLayout.LEFT);
      add(panel2, FlowLayout.CENTER);
      add(panel3, FlowLayout.RIGHT);   

      ArrayList<Integer> list = new ArrayList<Integer>();
      for(int y = 0; y < 26; ++y)
      {
         list.add(new Integer(y));
      }
      Collections.shuffle(list);
      for(int y = 0; y <= 3; ++y)
      {
         panel1.add(buttons[list.get(y)]);
      }               
      for(int y = 4; y <= 7; ++y)
      {
         panel2.add(buttons[list.get(y)]);
      }       
      panel3.add(label);      
      setSize(500, 500);
   }

   public void actionPerformed(ActionEvent e)
   {
      Object source = e.getSource();
      if(source == buttons[0] || source == buttons[1] || source == buttons[2] || source == buttons[3] || source == buttons[4])
      {
         String type = "Vowel";
         label.setText(type);              
      }
      else
      {
         String type = "Consonant";
         label.setText(type);
      }   
   }

   public static void main(String[] args)
   {
      JVowelConsonant frame = new JVowelConsonant();
      frame.setVisible(true);
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jarid.wade
  • 19
  • 6
  • 2
    Instead of creating 26 `JButtons` why dont you change the text of clicked `JButton` with random alphabet that is not in the grid.use Button.setText("vlaue") to change the text – Madhan Jun 21 '15 at 13:59
  • 1
    Use `Collections.shuffle()` on a `List` to avoid duplicates. – trashgod Jun 21 '15 at 14:31
  • 1
    @trashgod They're all over that action, except they are using a list of **integers** as the indices of the `letter`. ;) – Andrew Thompson Jun 21 '15 at 15:11

0 Answers0