Guessing Game Exception Problem
I have a problem with my code... I can't tell why but the problem says:
addActionListener(java.awt.event.ActionListener) in javax.swing.JTextField cannot be applied to (RandomGuess2) --line 56
I'm quite a newbie in the world of Java so can anyone give a hand? Don't mind the pics yet... What's important is the one in JTextField.. >.>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
publicclass RandomGuess2extends JPanel{
JCheckBox dress;
JCheckBox hair;
JCheckBox other;
JCheckBox face;
JTextField inguess;
int num1;
StringBuffer choices;
JLabel guesspic;
CheckBoxListen myListener =null;
public RandomGuess2(){
//myListener for the checkbox
myListener =new CheckBoxListen();
hair =new JCheckBox("clue 1");
hair.addItemListener(myListener);
other =new JCheckBox("clue 2");
other.addItemListener(myListener);
face =new JCheckBox("clue 3");
face.addItemListener(myListener);
dress =new JCheckBox("clue 4");
dress.addItemListener(myListener);
// Indicates what's on the geek.
choices =new StringBuffer("dhfr");//Default Image has all the parts.
// Set up the picture label
guesspic =new JLabel(new ImageIcon("haru-" + choices.toString().trim() +".jpg"));
guesspic.setToolTipText(choices.toString().trim());
// Put the check boxes in a column in a panel
JPanel jplCheckBox =new JPanel();
jplCheckBox.setLayout(new GridLayout(0, 1));//0 rows, 1 Column
jplCheckBox.add(hair);
jplCheckBox.add(other);
jplCheckBox.add(face);
jplCheckBox.add(dress);
//input text field for the final guess
inguess =new JTextField(10);
inguess.addActionListener(this);
JLabel choi =new JLabel("CHOICES: 1. Suzumiya Haruhi2. Fujioka Haruhi");
setLayout(new BorderLayout());
add(inguess, BorderLayout.NORTH);
add(jplCheckBox, BorderLayout.WEST);
add(guesspic, BorderLayout.CENTER);
add(choi, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
}
//Listens to the check boxes events
class CheckBoxListenimplements ItemListener{
publicvoid itemStateChanged(ItemEvent e){
int index = 0;
char c ='-';
Object source = e.getSource();
if (source == dress){
index = 0;
c ='d';
}elseif (source == hair){
index = 1;
c ='h';
}elseif (source == face){
index = 2;
c ='f';
}elseif (source == other){
index = 3;
c ='r';
}
if (e.getStateChange() == ItemEvent.DESELECTED)
c ='-';
choices.setCharAt(index, c);
guesspic.setIcon(new ImageIcon("haru-" + choices.toString().trim() +".jpg"));
guesspic.setToolTipText(choices.toString());
}
}
publicvoid actionPerformed(ActionEvent evt)
{
String rest ="";
try{
num1 = Integer.parseInt(inguess.getText());
if(num1 == 1){rest ="CORRECT!!";}
else{rest ="WRONG!!";}
}
catch(NumberFormatException numFormExcept){
JOptionPane.showMessageDialog(this,"You must enter either 1 or 2","Invalid Format", JOptionPane.ERROR_MESSAGE);
}
}
publicstaticvoid main(String s[]){
JFrame fame =new JFrame("JCheckBox Usage Demo");
fame.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
fame.setContentPane(new RandomGuess2());
fame.pack();
fame.setVisible(true);
}
}

