lundi 11 septembre 2017

Java-Assign string within array to JButton randomly within a for loop. Each element once

 public static void main(String[] args){
     String[] word = {" The ", " itsy " , " bitsy ", " spider ", " went " , 
" up " , " the ", " water ",
" spout. ", " Down ", " came ", " the " , " rain ", " and ", " flushed ", 
" the ", " spider ", " out. "};
     Random random = new Random();

     JFrame frame = new JFrame();
     frame.setLayout(new BorderLayout());
     frame.setTitle("JavaGame");
     frame.setSize(600,600);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JPanel northPanel= new JPanel(new GridLayout(3,6));
     for (int i=0; i<18; i++) {
         JButton button = new JButton();
         northPanel.add(button);
         button.setText(word[random.nextInt(word.length)]);
     }

Disclaimer:I'm a noob here.I'm looking for something to plug in to make sure each element only populates once




Aucun commentaire:

Enregistrer un commentaire