Here is the jframe above,images selected at random,the task is after image is displayed,choose from jcombo the answer,press submit and display in jtextfield below the answer:"Correct" or not.And untill the submitt button is not pressed ,disable the next button.How would i do this logic? So far i have this done:
package my.GQuiz;
import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
picLabel = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jTextFieldAnswer = new javax.swing.JTextField();
jButtonSubmit = new javax.swing.JButton();
jButtonNextFlag = new javax.swing.JButton();
jLabelStatic = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Geography Quiz");
picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
picLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/my/GQuiz/images/Brazil.png"))); // NOI18N
picLabel.setBorder(javax.swing.BorderFactory.createTitledBorder("Flag"));
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " Australia", " Brazil", " China", " Italy", " Russia", " South Africa", " Spain", " United States" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jTextFieldAnswer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldAnswerActionPerformed(evt);
}
});
jButtonSubmit.setText("Submit");
jButtonSubmit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSubmitActionPerformed(evt);
}
});
jButtonNextFlag.setText("Next Flag");
jButtonNextFlag.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNextFlagActionPerformed(evt);
}
});
jLabelStatic.setText("Select Country:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(picLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextFieldAnswer)
.addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 162, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabelStatic)
.addGap(0, 0, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButtonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonNextFlag))
.addGap(31, 31, 31))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonSubmit)
.addComponent(jLabelStatic))
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(picLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonNextFlag))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(81, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
String[]imageNames={"Australia.png","Brazil.png","China.png","Italy.png","Russia.png","South Africa.png","Spain.png","United States.png"};
String[]imageNamesAnswer={"Australia.png","Brazil.png","China.png","Italy.png","Russia.png","South Africa.png","Spain.png","United States.png"};
int ans =0;
Random rand = new Random();
boolean[] questions_Asked = new boolean[8];
private void jButtonNextFlagActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int index = rand.nextInt(8);
Image im=new ImageIcon(this.getClass()
.getResource("/my/GQuiz/images/" + imageNames[index])).getImage();
ImageIcon iconLogo = new ImageIcon(im);
picLabel.setIcon(iconLogo);
}
private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {
for(int i=1; i<imageNames.length; i++)
{
int randomNum = rand.nextInt(8);
while (questions_Asked[randomNum] == true)
{
randomNum = rand.nextInt(8);
}
// String str = String.valueOf(randomNum);
if(imageNames[i] == imageNamesAnswer[i]){
jTextFieldAnswer.setText("Correct");
}
else{
jTextFieldAnswer.setText("No");
}
}
}
private void jTextFieldAnswerActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonNextFlag;
private javax.swing.JButton jButtonSubmit;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabelStatic;
private javax.swing.JTextField jTextFieldAnswer;
private javax.swing.JLabel picLabel;
// End of variables declaration
}