I'm using netbeans btw if you notice the long code for the GUI
I'm doing a quiz game. The questions are randomized.
My problem is, the randomized questions do not appear on the questionLabel (JLabel Variable). I don't know what to do.
Help?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kiddiequiz.scienceforms;

import java.awt.Color;
import java.util.Random;

/**
 *
 * @author USER
 */
public class qsez1 extends javax.swing.JFrame {

    /**
     * Creates new form qsez1
     */
    public qsez1() {
        initComponents();
         getContentPane().setBackground(new Color(255,51,51));
        setResizable(false);



    }                                                                      

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        questionLabel = new javax.swing.JLabel();
        choice1 = new javax.swing.JButton();
        choice2 = new javax.swing.JButton();
        choice3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        questionLabel.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        questionLabel.setForeground(new java.awt.Color(255, 204, 204));

        choice1.setBackground(new java.awt.Color(255, 51, 51));
        choice1.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice1.setForeground(new java.awt.Color(255, 153, 102));
        choice1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice1ActionPerformed(evt);
            }
        });

        choice2.setBackground(new java.awt.Color(255, 51, 51));
        choice2.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice2.setForeground(new java.awt.Color(255, 153, 102));
        choice2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice2ActionPerformed(evt);
            }
        });

        choice3.setBackground(new java.awt.Color(255, 51, 51));
        choice3.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
        choice3.setForeground(new java.awt.Color(255, 153, 102));
        choice3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choice3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(questionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 907, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(137, 137, 137)
                                .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(70, 70, 70)
                                .addComponent(choice2, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(295, 295, 295)
                                .addComponent(choice3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(questionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(choice1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(choice2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(choice3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>                        

    private void choice1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void choice2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void choice3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

  public void questionrandomizer(){
       String qez[] ={"What is an animal that eats other animals called?", //[1] ans: predator
                       "During which stage does a caterpillar become a chrysalis?", // [2] ans: Pupa
                       "What do you call a scientist that studies life?", // [3] ans: biologist
                       "What do you call the series of changes that many animals go through in life?", // [4] life cycle
                       "An animal that changes colors?"}; // [5] ans: chameleon/YANDERE
       String listEZ;
       Random random = new Random();
      /* int index = random.nextInt(qez.length);
       Integer.toString(index);*/
       listEZ=qez[random.nextInt()];


       if(listEZ==qez[0])
        {
           questionLabel.setText(qez[0]);
           choice1.setText("Prey");
           choice2.setText("Predator");
           choice3.setText("Meanie");
        }

       if(listEZ==qez[1]){
           questionLabel.setText(qez[1]);
           choice1.setText("Prey");
           choice2.setText("Predator");
           choice3.setText("Meanie");
       }

       if(listEZ==qez[2]){
           questionLabel.setText(qez[2]);
           choice1.setText("Prey");
           choice2.setText("Predator");
           choice3.setText("Meanie");
       }

  }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(qsez1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new qsez1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton choice1;
    private javax.swing.JButton choice2;
    private javax.swing.JButton choice3;
    private javax.swing.JLabel questionLabel;
    // End of variables declaration                   
}

Recommended Answers

All 6 Replies

questionrandomizer() isn't going to do anthing because you never call it.

i tried calling it on a mouseEvent yet it doesn't appear.

I can't comment on yur code becuase I don't have the actual version you are running, so it's time for you to do some debugging...
Add print statements to your code so you can see what is being executed and what is not. Eg one at the start of questionrandomiser, one inside each of its if blocks etc.

it printed all the elements inside the array. i just want one random element to be printed

my mistake. it was just one. i thought it was all elements because i used a mouseEntered event

OKAY I FIXED IT! THANK YOU SO MUCH. AHAHAHHA

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.