I have the HangGame program I wont write this code when I Press any Latter Button like (A,B,….. ) the latter that I Pressed it will be appear in the right place of the label part of the word that I read it in the text file for example I read word in Text file like (Stake) and on the screen will appear five label (_ _ _ _ _ ) , when I hold S button the character S will appear on the first label part (S _ _ _ _ ) and so on… . I don’t know how I write the code for the Action listener inside Button. finally I add Labels with out problem but I don’t know how to put right character in the right place.
I hope you understand my problem.
public void ButtonComponent () {
for (int i = 65; i < 78; i++) {
JButton temp = new JButton("" + (char) i);
temp.addActionListener(new BtnListener());
temp.setBounds((i - 64) * 55, 110, 50, 30);
add(temp);
}
for (int i = 78; i < 91; i++) {
JButton temp = new JButton("" + (char) i);
temp.addActionListener(new BtnListener());
temp.setBounds((i - 77) * 55, 150, 50, 30);
add(temp);
}
}
class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int s=0;s<splitword.length;s++) {
String btnText = ((JButton)e.getSource()).getText().toLowerCase();
if (splitword[s].contains(btnText)) {
System.out.println("This letter is contained in the word:" + btnText);
}
As this is Hangman, you will have to keep track of all correct letters used. Here is a template for a solution:
I won’t solve the problem outright but this should give you enough to work with.