i have this code in java...
my first combobox when u choose letter a will be correct...
but how will apply in the other combobox that when u choose it...
the letter b will be the correct answer??
plzz help me...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CaseStudyFirst implements ItemListener{
JComboBox choice1, choice2;
int score;
public CaseStudyFirst(){
JFrame f = new JFrame();
f.setSize(500, 500);
f.setLayout(new GridBagLayout());
GridBagConstraints x = new GridBagConstraints();
Container con = f.getContentPane();
// JLabel 2 be put here....
JLabel label1 = new JLabel("Name: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.insets = new Insets(3, 2, 2, 3);
x.gridx = 0;
x.gridy = 0;
con.add(label1, x);
JLabel label2 = new JLabel("Section: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.weighty = 0.0;
x.gridx = 0;
x.gridy = 1;
con.add(label2, x);
JLabel question1 = new JLabel("Questions: ");
x.fill = GridBagConstraints.HORIZONTAL;
x.gridx = 0;
x.gridy = 2;
con.add(question1, x);
//JTextField to be put here...
JTextField TextField1 = new JTextField();
x.gridx = 1;
x.gridy = 0;
x.ipadx = 100;
x.ipady = 1;
x.gridwidth = 5;
TextField1.setToolTipText("Enter Your Name Here: ");
con.add(TextField1, x);
JTextField TextField2 = new JTextField();
x.gridx = 1;
x.gridy = 1;
x.ipadx = 100;
x.ipady = 1;
x.gridwidth = 5;
TextField2.setToolTipText("Enter Your Section Here: ");
con.add(TextField2, x);
choice1 = new JComboBox(new Object[]{"a","b","c","d"});
x.gridx = 0;
x.gridy = 3;
con.add(choice1, x);
choice1.addItemListener(this);
choice2 = new JComboBox(new Object[]{"a","b","c","d"});
x.gridx = 0;
x.gridy = 5;
con.add(choice2, x);
choice2.addItemListener(this);
f.pack();
f.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == choice1) {
if(choice1.getSelectedItem().equals("a")) {
System.out.println("CORRECT!"); }
else {
System.out.println("WRONG!");
}
choice1.setEnabled(false);
}
}
)
public static void main(String[] args) {
new CaseStudyFirst();
}
}