プログラムの一部を取り出して、ここにコードを置きます。基本的に問題は、アイテム リスナー メソッドが呼び出されていないことです。ここで、ユーザーが正しい答えを選択して「送信」を押すと、actionlistener メソッドが答えが正しいかどうかを知らせます。
static boolean answer = false; // returns this as true if user got question right
public void QuizPanel() {
QuizPanel = new JPanel();
QuizPanel.setLayout(null); // default layout
JLabel label = new JLabel( "True/False- Java is Object Orientated?");
label.setBounds(10, 50, 400, 20);
QuizPanel.add(label);
option1 = new JCheckBox("True");
option1.setSelected(false);
option1.setBounds(10, 70, 120, 40);
QuizPanel.add(option1);
option1.addItemListener(this); // Adds a item listener
option2 = new JCheckBox("False");
option2.setSelected(false);
option2.setBounds(40, 70, 120, 40);
QuizPanel.add(option2);
option2.addItemListener(this);
///////////////////////////////////////
JButton submit = new JButton("SUBMIT ANSWER:");
submit.setBounds(10, 100, 150, 20);
QuizPanel.add(submit);
submit.addActionListener(new ActionListener() { // Action Listener
public void actionPerformed(ActionEvent evt2) {
System.out.println ("Your answer is " + answer); // Got it right?
}
});
}
public void itemStateChanged1(ItemEvent e1) {
Object source = e.getItemSelectable();
if (source == option1) {
answer = true;
} else {
//nothing- stays false
}
}
何も起こらず、ユーザーが正しいか間違っているかをユーザーに伝えません。