简体   繁体   中英

JButton Display text in JLabel

I develop a game

In my first button, I write a word. in my second button , I enter a letter

I would like to find the word I register with my first button. My problem is that I can not back up previous research that I already do.

I do not understand how I could do, so maybe you could help me, it will be great! Sorry for my approximativ

Example :

if I write a word with my first jButton : "hello" my program return _ _ _

If I search "h", with my second button the program return : h _ _ _ _ // in my labelword

but now, if I search "e" the program return e _ _ _, the "h" is not repost.

My code, First Jbutton :

ControllerPendu a = new ControllerPendu();
final JFrame popup = new JFrame();
this.word = JOptionPane.showInputDialog(popup, "Enter one word", null);

//control lenght of word, if the word < = 3 
String afterCtrl = a.controleW(word);


labelWord.setText("");
for (int i = 0; i < afterCtrl.length(); i++) {
    labelWord.setText(labelWord.getText() + "_ ");
    labelWord.validate();
    labelCompteur.repaint();
}

//counter key on the word, panel top/right
String mot = this.word;
labelCompteur.setText(String.valueOf(mot.length()));

and the 2nd jButton :

ControllerPendu a = new ControllerPendu();

final JFrame popup = new JFrame();
this.key = JOptionPane.showInputDialog(popup, "Enter one key", null);

char ctrlKey = a.controleK(key).charAt(0);     
// loop for print key of the word
for (int i = 0; i < this.word.length(); i++) {

    //retourn the key in function to the index 
    char lettre = word.charAt(i);


    if (ctrlKey == lettre) {
        labelWord.setText(key);

    } else  {
        labelWord.setText(labelWord.getText() + "_ ");

    }
}

    } else  {
        //is the ke isn't match, display "_ " 
        labelWord.setText(labelWord.getText() + "_ ");

    }
}

ControllerPendu is the class is to check the word and the letter ctrlKey = the key tiped in second jButton

Why don't you store the search result in some instance variable String searchResult , then you can update it each time a new search has place.

Sorry, I wish I could post in how to do that, but your code is missing some parts and can't make a wild guess on top of what you have already forwarded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM