1

Someone from stackoverflow helped me with my code for buttons by giving me the right code that's also more appropriate... But the problem is I want to use this code in a hangman program, when someone clicks on the button it must scan a word and print either "Yes, the letter is in the word" or "NO, the letter is not in the word"

I came across some code in stackoverflow that does that, but I have no idea how to adapt that persons code to my button code

The code for creating buttons(as I got it):

public CharSearch(){

super(BoxLayout.Y_AXIS);
    for(char i = 'A'; i <= 'Z'; i++){
        String buttonText = new Character(i).toString();
        JButton button = getButton(buttonText);
        add(button);
    }
}

public JButton getButton(final String text){
    final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "You have clicked: "+text);
            //If you want to do something with the button:
            button.setText("Clicked"); // (can access button because it's marked as final)
        }
    });
    return button

and then the code I found (I'll also post the link):

if(original.indexOf(button)!=-1){
    JOptionPane.showConfirmDialog(panel, "Your word does contain" + button );
}
else{
    JOptionPane.showConfirmDialog(panel, "There is no" + button );
}

How can I check if a single character appears in a string?

Question is how do I make the two codes work together, I Have tried fusing them but then i get errors with the indexOf at

if(original.indexOf(button)!=-1){

I'll also post my full code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public final class CharSearch extends Box{
int i =0;
int error = 0;
static JPanel panel;
String original = "Dinosaur";
JLabel label = new JLabel();
String secret = new String(new char[original.length()]).replace('\0', '-');

public CharSearch(){

super(BoxLayout.Y_AXIS);
    for(char i = 'A'; i <= 'Z'; i++){
        String buttonText = new Character(i).toString();
        JButton button = getButton(buttonText);
        add(button);
    }
}

public JButton getButton(final String text){
    final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "You have clicked: "+text);
            //If you want to do something with the button:
            button.setText("Clicked"); // (can access button because it's marked as final)
        }
    });
    return button;

    if(original.indexOf(button)!=-1){
    JOptionPane.showConfirmDialog(panel, "Your word does contain" + button );
    }
 else{
    JOptionPane.showConfirmDialog(panel, "There is no" + button );
 }

}

public static void main(String[] args){
    EventQueue.invokeLater(new Runnable(){
        public void run(){
           JFrame frame=new JFrame();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setContentPane(new CharSearch());
            frame.pack();
            frame.setVisible(true);
            new CharSearch();
        }
    });
}
}
Community
  • 1
  • 1
Night Programmer
  • 237
  • 1
  • 5
  • 14

1 Answers1

0

Change button to button.getText() (assuming the text of the button is just like b or c), or possibly getName if you have them all named by their character (that may be better as it allows you to use different stuff for the text the button shows, like the button could be 'Choose c', but the name 'c', and it would still work)

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31